Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Is this type of chain feasible? #155

Closed
thedeeno opened this Issue · 4 comments

3 participants

@thedeeno

Thanks for this lib! Using it has been very fun so far.

Curious, is it possible to create a chain like this?

expect(my_function).to.render(template).exactly(3).times

I have this assertion helper working:

expect(my_function).to.render(template)

but I'd LOVE to extend it with a more narrow assertion when I continue to chain .exaclty(count).times. Does someone more experienced know if this is possible? This additional chaining is a little different than the typical semantics. I'm not sure if I'm breaking convention.

@domenic
Owner

This is definitely possible. Consider:

expect(myArray).to.have.length(3).and.deep.equal(5)

The thing to realize is that each part of the chain is just Chai evaluating some code. Some pieces evaluate no real code, e.g. to, have, and. Some pieces set flags for later assertions to deal with, e.g. deep. And some pieces perform assertions, i.e. test something about the object and maybe throw an assertion error.

So in your case, I'd make render set a flag saying "when exactly is called, test the number of times rendered." And then exactly would check for that flag, and if it's set, do the number-of-times-rendered assertion. Then times could be a no-op.

Alternately render and exactly could both set flags that times uses to do the assertion.

@thedeeno

Nice. Thanks for the reassurance.

It looks like helpers are designed to be either 'asserters' or 'flaggers' and not both. Am I wrong?

In other words, it looks like

// I can't make this #render an assertion
expect(my_function).to.render(template)

// while also making this #render set a flag
expect(my_function).to.render(template).exactly(3).times

I was hoping there was some chain context and delayed evaluation. Is there?

Now I'm using the following as a work around

// assert template was rendered at least once
render(my_template)
// asserts template was rendered 3 times
render(my_template, { exactly: 3 }).times 

but the original design would be my ideal.

@domenic
Owner

They can definitely be both. Just set a flag, and do an assertion, inside the function.

@keithamus
Owner

Hey @thedeeno, hopefully you're happy with the answers @domenic has given you. I'm going to close this issue now but if you have any other questions don't hesitate to open a new issue, or alternatively you can ask on our Google Group: https://groups.google.com/forum/?fromgroups=#!forum/chaijs

@keithamus keithamus closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.