Should.js binds multiple statements on one property

I have an object like this:

var obj = {
    "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51",
    "type": "candy"
}

I want to write a test that first checks that the object has the โ€œuuidโ€ property first, and then โ€œuuidโ€ is a specific length (36 characters).

Trying this does not work

obj.should.have.property('uuid').which.should.have.length(36)

Failure:

Uncaught AssertionError: expected Assertion {
  obj: '60afc3fa-920d-11e5-bd17-b9db323e7d51',
  params: { operator: 'to have property \'uuid\'' },
  negate: false } to have property 'length' of 36 (got [Function])

And this (which does not actually make syntactical meaning anyway), since it applies to the parent object, not the value)

obj.should.have.property('uuid').and.be.length(36)

Failure:

Uncaught TypeError: usergridResponse.entity.should.have.property(...).which.should.be.equal.to is not a function

Even this does not work:

obj.should.have.property('uuid').which.equals('60afc3fa-920d-11e5-bd17-b9db323e7d51')

So, what is the correct way to associate statements about a property of an object?

+4
source share
2 answers

I think this might be the best option:

var session = {
    "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51",
    "type": "candy"
};

session.should.have.property('uuid').with.a.lengthOf(36);

, should, , ( ).

var session = {
    "uuid": "60afc3fa-920d-11e5-bd17-b9db323e7d51",
    "type": "candy"
};

session.should.have.property('uuid').which.obj.should.have.length(36);

, :

https://jsfiddle.net/Lz2zsoks/

.an, .of, .a, .and, .be, .have, .with, .is, .which - , .

@denbardadym, , should :

  • ,
  • Should.js . .
+3

, .should - , , :

obj.should.have.property('uuid').which.have.length(36)

( sais, Assertion {...} )

:

obj.should.have.property('uuid').and.be.length(36)

( , )

- .equals .equal. , "0320a79a-920d-11e5-9b7a-057d4ca344ba" !== "60afc3fa-920d-11e5-bd17-b9db323e7d51"

, .

+1

Source: https://habr.com/ru/post/1617116/


All Articles