WRITING CUSTOM RSPEC MATCHERS

You should start writing custom matchers as soon as possible in a project. Test code is code like any other code, which needs to be maintained. Define a custom matcher rspec-expectations provides a DSL for defining custom matchers. It therefore needs to be well-factored and use the domain language. Accepting a string in our match block — rather than a response object — helps keep our matcher generic enough that we can re-use it later. However, the APIs can differ e. Our matcher is quite useful, but we could make it neater.

We can introduce a helper method to make things pretty again:. May your tests now be more readable…. More about adding a collaborator. It all boils down to coming up with a true or false response to indicate whether the test passed or failed. You can find more information on defining fluent, chained matchers, diffable matchers and accepting blocks as arguments in the RSpec documentation. Soon you’ll be able to also add collaborators here! This gave us the opportunity to use the much terser ternary operator and split out the fetching of the status code from the matcher comparison.

This example has only demonstrated the basics of RSpec matchers. This is admittedly more code, and arguably less obvious than the DSL-version — but it is easier to refactor.

This should demonstrate the difference between a generic, re-usable matcher to writign implementation details from the reader of the code, and modelling your domain language so you can write human-friendly specifications. It all boils down to coming up with a true or false response to indicate whether the test passed or failed. Test code needs to be both run by computers and read by humans.

  NRICH PROBLEM SOLVING KS1 MULTIPLICATION

writing custom rspec matchers

Matchers can marchers written as plain old Ruby objects, as long as they conform to a specific API — methods named like the blocks in our previous example. You may want to check that a HTTP response has a particular status code. Make sure your matcher returns either true or false. More about adding a collaborator. Next post Previous post. You can find more information on defining fluent, chained matchers, diffable matchers and accepting blocks as arguments in the RSpec documentation.

To get into this habit, try to limit yourself to a maximum of three lines per test: If you need to write a more complex matcher and would like to use the Class -approach yourself, please head over to our API -documentation and read the docs about the MatcherProtocol.

If you want your matcher to be readable in different contexts, you can use the. It therefore needs to be well-factored and use the domain language. Home Feed About Archives github twitter. Test code is code like any other code, which cuwtom to be maintained. This is the place where we implement our custom logic:.

You might argue that this matcher is not quite high-level enough to actually model our rrspec. We want to assert that sensitive information is omitted from the JSON document for unauthorised users.

define matcher

It is probably a good idea to tighten up that if statement with an elsif for Rack:: Note that we can change the block dustom names to match our domain. You should handle each StandardError with care!

  CURRICULUM VITAE XIMO PUIG

writing custom rspec matchers

RSpec dumps the actual and expected values, and combines them with the name of our matcher to create a generic error message. Would you like to learn how to build sustainable Rails apps and ship more often?

writing custom rspec matchers

We define our own domain-specific assertions and use them to compose readable specifications. This matcher is also just a Ruby class, for which you could write tests if you wanted to go really meta. So, class-based matchers usually come with helper methods to make specifications readable and hide implementation mstchers from the reader.

Our matcher is basically ready for use, but we can do better. In such cases it would rwiting a good idea to use the same custom matcher defined above for all cases DRY.

Writing Custom RSpec Matchers – Timothy’s Blog

It all boils down to coming up with a true or false response to indicate whether the test passed or failed. Our matcher is basically ready for use, but we can do better. Custpm believe you should start writing custom matchers as soon as possible in a project. To add a collaborator to this project you will need to use the Relish gem to add the collaborator via matdhers terminal command.

The specs are fair enough, but there are two different levels of abstraction in play here: Custom RSpec matchers can help with this problem.

Author: admin