tj / should.js

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

`or` concatenation #91

Closed buschtoens closed 11 years ago

buschtoens commented 11 years ago
myObj
  .should.be.a("string")
  .or.be.an.instanceOf(Array);

Would be quite useful.

tamsler commented 11 years ago

I am looking for something like:

e.g. response.should.have.status(201).or(409);

buschtoens commented 11 years ago

So .or should be a function that, when called performs the last check again with the new supplied argument, or performs the appended checks. Nice sugar.

myObj
  .should.be.a("string")
  .or("number");               // redo type check

myObj
  .should.be.a("string")
  .or.be.an.instanceOf(Array); // do the appended check

myObj
  .should.be.a("string")
  .or("number")                // redo type check
  .be.an.instanceOf(Array);    // do the appended check
tj commented 11 years ago

or seems a bit odd for a test that should be deterministic

buschtoens commented 11 years ago

On first glance yes. But think about APIs that may return a value or null, if no data was found.

tj commented 11 years ago

you should have a separate test case for those scenarios

buschtoens commented 11 years ago

Ah yes, that's true.

jtmarmon commented 10 years ago

thoughts on re-opening this? I have a case where I think it would be useful. My application needs to retrieve data, and that data can be either XML or JSON, so being able to do

response.should.have
.header("content-type", "application/json")
.or("content-type", "application/xml");

I can set up a PR if we agree it would be a beneficial addition

btd commented 10 years ago

well, it is still unclear - even in your case response either json or xml, and it depends from actual request - so it is separate tests. From technical point it is very unclear how .or should work, because as soon .header executed and failed - it will throw error and .or will not be executed.

jtmarmon commented 10 years ago

@btd but I don't know what the request is going to return. the modules I wrote to parse the data (not being tested here) accept XML or JSON, so all I need to do in the test is ensure that they return the proper data.

It does seem a bit complicated on the technical side. I haven't delved too much into the codebase yet, but it may be worth looking at how q.js handles .catch.

kristjan commented 10 years ago

Your tests should know which of XML or JSON is supposed to come back given an input. If that's nondeterministic, I'd wager you're going to have larger issues :)

jtmarmon commented 10 years ago

The issue is that data providers in my particular case can give XML or JSON, and dealing with that data is a job for a separate adapter behavior that I didn't want to test there. I suppose I can add a property tagging it as an XML or JSON provider.

kristjan commented 10 years ago

The concern I'd have with a test checking xml || json is that the controller could report a Content-Type that doesn't match what your provider generated. I'd totally build two tests and stub or specify the format it's returning in each.

jtmarmon commented 10 years ago

good point. thanks! :)