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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With