Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When testing with rspec, where to put common "test utility methods"?

Tags:

Suppose you have a shopping site that sells widgets. However, the inventory of each widget is limited, so it's important to keep the "widget.number_still_available" number up to date.

I'd like to write an rspec test along the lines of

it "always displays the correct number still available" do     # Assume there is a before method that sets up a widget with 5 available     widget.number_still_available.should == 5     # User "[email protected]" purchases 2 widgets    widget.number_still_available.should == 3     # User "[email protected]" purchases 1 widget    widget.number_still_available.shhould == 2     # User "[email protected]" cancels purchase of 1 widget    widget.number_still_available.should == 4 end 

I'd like to be able to write testing-only methods that performs the "purchasing" and "canceling" methods. These actions don't correspond to any "real" methods in my models for a variety of reasons (most significantly there is a decoupled back-end system in PHP that performs part of the purchasing and canceling actions).

Where is the correct place to put this code when using RSpec? In cucumber, I could write a couple of steps - but I'm not sure what the correct equivalent is for RSpec.

like image 646
cailinanne Avatar asked Nov 14 '11 19:11

cailinanne


People also ask

Where do you put test utilities?

If an utility method is used only in a single testcase class, put it right there. If it's used in several testcase classes across a single project, put it into a separate test helper class. If it's used more than in one project, put it into a separate test utility lib.

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

What RSpec method is used to create an example?

The it Keyword The word it is another RSpec keyword which is used to define an “Example”. An example is basically a test or a test case. Again, like describe and context, it accepts both class name and string arguments and should be used with a block argument, designated with do/end.

Is RSpec BDD or TDD?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


2 Answers

I would suggest making a new file in spec/support called purchase_helpers.rb and put this content in it:

module PurchaseHelpers   def purchase_widgets(user, count=1)     # Code goes here   end    def cancel_purchase(user, count=1)     # Code goes here   end end  RSpec.configure do |c|   c.include PurchaseHelpers end 

The benefit of doing this rather than chucking it into spec/spec_helper.rb is that it is not crowding up that file with a lot of unrelated-to-the-setup-of-RSpec code. Separating things out is the better way to do things.

like image 130
Ryan Bigg Avatar answered Oct 13 '22 00:10

Ryan Bigg


You can drop a monkeypatch into spec_helper.rb, or directly at the top of the spec file if it's only used for that one file.

It would be more clear and safe to make helper methods which use existing class methods, instead of monkeypatching the classes.

like image 44
John Bachir Avatar answered Oct 12 '22 22:10

John Bachir