WRITING CUSTOM RSPEC MATCHERS

So, class-based matchers usually come with helper methods to make specifications readable and hide implementation details from the reader. Our matcher is basically ready for use, but we can do better. This matcher is also just a Ruby class, for which you could write tests if you wanted to go really meta. The specs are fair enough, but there are two different levels of abstraction in play here: Next post Previous post.

However, the APIs can differ e. In such cases it would be a good idea to use the same custom matcher defined above for all cases DRY. We can improve our matcher further with a custom exception message. Matchers 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. To add a collaborator to this project you will need to use the Relish gem to add the collaborator via a terminal command. Our matcher is basically ready for use, but we can do better.

We want to assert that sensitive information is omitted from the XML for unauthorised users. Our matcher is basically ready for use, but we can do better. We define our own domain-specific assertions, and use them to compose readable specifications.

Extending our custom matcher further Our custom matcher does the job but there are some potential problems with it. More about adding a collaborator. Would you like to learn how to build sustainable Rails apps and ship mahchers often? Close To add a collaborator to this project you will need to use the Relish gem to add the collaborator via a terminal command.

  ESSAY ABOUT NSTP CWTS

Defining Custom Matchers in RSpec

Test code needs to be both run by computers and read by humans. Next post Previous post.

writing custom rspec matchers

Matchers 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.

Wriiting sure your matcher returns either true or false. The specs are fair enough, but there are two different levels of abstraction in play here: We could write such a test as follows:. You should start writing custom matchers as soon as possible in a project.

We can introduce a helper method to make things pretty again:. You should handle each StandardError with care! However, the APIs can differ e.

Writing custom RSpec matchers by Arjan van der Gaag

It all boils down to coming up with a true or false response to indicate whether the test passed or failed. Do not handle them all in one.

You may want to check that a HTTP response has a particular status code. Learn more and download a free copy. This is the place where we implement our custom logic:.

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. In such cases it would be a good idea to use the same custom matcher defined above for all cases DRY. To get into this habit, try to limit yourself to a maximum of three lines per test: It is probably a good idea to tighten up that if statement with an elsif for Rack:: Our matcher is quite useful, but we could make it neater. XML str end end. Which is not as nice as with our DSL -version.

  SOUTHRON AMBITIONS ESSAY

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. You might argue that this matcher is not quite high-level enough to actually model our domain. Defining custom RSpec matchers is really easy and a great way to increase the readability of your tests.

writing custom rspec matchers

We can therefore create a new file. To add a collaborator to this project you will need to use the Relish gem to add the collaborator via a terminal command.

writing custom rspec matchers

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.