Buy @ Amazon

Search This Blog

October 21, 2011

Leveraging Subject in RSpec - A rough cut!

Do you use subject in RSpec? This blog post intends to help you get better with RSpec's subject. The better way to learn things is by way of an example; even better if we see a bad example and see how we can progressively improve it.



The better way to write the above test is as below:



Further refactoring the above code snippet, it can be compacted to below, although I hate to do this. I prefer to tests/specs being an utter no-brainer to read quickly and get the idea:



Finally the ideal way of testing the above scenario and the like would be to employ RSpec's subject as below:



If after reading all this you were to wish it be more leaner, then you should consider shoulda. Will blog about it's marriage with RSpec sooner ;) Until then see how snappier the above code becomes with the use of shoulda matchers. Hooray!!!