Remember Using contracts.ruby With RSpec ?
RSpec mocks violate all :class contracts because is_a?(ClassName) returns
false for mock. That post describes 2 possible solutions:
- stub
:is_a?:allow(my_double).to receive(:is_a?).with(MyClass).and_return(true), or - use
contracts-rspecgem, that patchesinstance_doubleRSpec helper.
Custom validators
Since custom validators have finally landed here:
egonShiele/contracts.ruby#159,
now you can just override :class validator to accept all RSpec mocks:
1 2 3 4 5 6 7 | |
Now, RSpec mocks will not violate all the :class contracts.
More information can be found here: Providing your own custom validators.
Additionally this refactoring enabled valuable speed optimization for complex contracts - validators for them will be evaluated only once and memoized.
Links
- Previous part
- contracts.ruby: https://github.com/egonSchiele/contracts.ruby
- contracts.ruby chat: https://gitter.im/egonSchiele/contracts.ruby
- Mentioned PR: https://github.com/egonSchiele/contracts.ruby/pull/159
- Introduction to contracts.ruby
- Great contracts.ruby tutorial
- Full fledged documentation PR: see here staging docs: https://relishapp.com/contracts-staging/contracts-ruby/docs
Thanks!
If you have any questions, suggestions or just want to chat about how contracts.ruby is awesome, you can ping me on twitter @tdd_fellow. If you have any issues using contracts.ruby you can create issues on corresponding github project. Pull requests are welcome!
Comments on hackernews.
Happy coding! @tdd_fellow on twitter.