We want to assert that sensitive information is omitted from the XML for unauthorised users. Next post Previous post. ArgumentError – occures, but there might be edge cases where you want to pass the exception to the user. We define our own domain-specific assertions and use them to compose readable specifications. We could write the above matcher as a class as follows:.
This should demonstrate the difference between a generic, re-usable matcher to hide implementation details from the reader of the code, and modelling your domain language so you can write human-friendly specifications. We can introduce a helper method to make things pretty again:. It all boils down to coming up with a true or false response to indicate whether the test passed or failed. Take care to handle exceptions appropriately in your matcher, e. You might argue that this matcher is not quite high-level enough to actually model our domain.
Behind the scenes RSpec:: You should start writing custom matchers as soon as possible in a project. This matcher is also just a Ruby matcers, for which you could write tests if you wanted to go really meta.
Writing custom RSpec matchers
So, class-based matchers usually come with helper methods to make specifications readable and hide implementation details from the reader. We define our own domain-specific assertions, and use them to compose readable specifications.
Note that we can change the block argument names to match our domain. RSpec will, by default, load all Ruby files under.
Home Feed About Archives github twitter. 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.
Writing custom RSpec matchers by Arjan van der Gaag
For anything non-trivial, it is nice to just deal with plain Ruby. We maatchers write such a test as follows:. There are two ways to implement matchers: This is the place where we implement our custom logic:. Rspce example has only demonstrated the basics of RSpec matchers.
Would you like to learn how to build sustainable Rails apps and ship more often? RSpec will report the following:. This would be a fair point, and whenever this particular itch comes up, you could scratch it not with a more specific matcher, but with another helper method around your generic matcher:.
define matcher – Custom matchers – RSpec Expectations – RSpec – Relish
RSpec dumps matchsrs actual and expected values and combines them with the name of our matcher to create a generic error message. You might argue that this matcher is not quite high-level enough wrtiing actually model our domain.
You may want to check that a HTTP response has a particular status code. This example has only demonstrated the basics of RSpec matchers. We could write the above matcher as a class as follows:.
The DSL-version looked much better. Note that we can change the block argument names to match our domain. The specs are fair enough, but there are two different levels of abstraction writibg play here: We can improve our matcher further with a custom exception message.
Writing Custom RSpec Matchers
To do so, it is easier to first convert it to a Ruby class and bypass the matcher DSL altogether. Soon you’ll be able to also add collaborators here!
Next post Previous post. This example uses RSpec 3. Matcherss can be written as plain old Ruby objects, as long as they conform to a specific API — methods named like the blocks in our previous example. It therefore needs to be readable and DRY. Custom RSpec matcheds can help with this problem. It all boils down to coming up with a true or false response to indicate whether the test passed or failed. Perhaps you are using more than one HTTP framework in your tests or – more likely – you are using the Rack:: Test code needs to be both run by computers and read by humans.