I've been using shoulda happily like this:
context "test world" do
setup do
@world = ...
end
should "be spinning" do
assert_equal "spinning", @world.movement
end
... and so on
end
I need a tip on understanding this variation:
class PostTest < Test::Unit::TestCase
should belong_to(:user)
should have_many(:tags).through(:taggings)
should validate_uniqueness_of(:title)
should validate_presence_of(:body).with_message(/wtf/)
should validate_presence_of(:title)
should validate_numericality_of(:user_id)
end
What's not clear to me is what the various "should belong_to(:user)" and so on are operating on. In other words, it appears pretty clear that they are operating on an instance of Post, but what is determinging that? What is the subject of the 'should xxx' lines?
I know this is a newbie question, so any pointer would be great!
shoulda defines a number of one-line assertions that can be used instead of a block to make certain kinds of test more concise. These are specific assertions about an Active Record model.
You are right that the subject of these assertions is a Post object. This is determined from the name of the test class. If you want to follow it through take a look at the source:
https://github.com/thoughtbot/shoulda/blob/master/lib/shoulda/context.rb
The construct_subject
method strips off the Test suffix and instantiates the resulting class name. And if you are curious about the specific single-line assertions for Active Record take a look at the Active Record matchers:
https://github.com/thoughtbot/shoulda/tree/master/lib/shoulda/active_record/matchers