tj / should.js

BDD style assertions for node.js -- test framework agnostic
MIT License
2.75k stars 194 forks source link

How to handle context? #125

Closed djMax closed 11 years ago

djMax commented 11 years ago

I want to use should in mocha. In those cases, I have some response objects (such as network responses) that I don't want to print out UNLESS something went wrong. I don't like doing "something".should.equal("foo", "Error from server: " + serverResponse). I'd rather do something LIKE:

should["response"] = serverResponse; "something".should.equal("foo", "Error from server");

and have it print out the context if an assert matches. I'm not wed to the particular syntax, just that you should be able to have a bunch of assertions without copying results every time. THoughts?

tj commented 11 years ago

not sure I get it, you want should.js to support response-specific auto-messages? why not just create a little wrapper function around should (or assert)

djMax commented 11 years ago

Sure, just seems like something that would be globally useful. The goal is that there are things you might want to output when an assertion fails but NOT if it doesn't (over a set of assertions). So you need some sort of container for those messages.

travisjeffery commented 11 years ago

@djMax can you write a concrete example with tests and expected output?

djMax commented 11 years ago

describe('something', function () { it('should call some server API', function () { request.post({url:"http://www.google.com"}, function (error, response, body) { should.not.exist(error); should.inspect(body); // This is what I'm asking for (something like it) so that IF an error happens now, it prints body body.indexOf("somestring").should.not.be.below(0); // lots more tests of characteristics of body would go here

   // Alternatively, it could be a closure type thing, though this feels odd
   should.inspect(body)(function () {
     body.indexOf("somestring").should.not.be.below(0);
   });
});

}); });