Shoulda::ClassMethods

Public Instance Methods

before_should(name, &blk) click to toggle source

Before statements

Before statements are should statements that run before the current context’s setup. These are especially useful when setting expectations.

Example:

 class UserControllerTest < Test::Unit::TestCase
   context "the index action" do
     setup do
       @users = [Factory(:user)]
       User.stubs(:find).returns(@users)
     end

     context "on GET" do
       setup { get :index }

       should respond_with(:success)

       # runs before "get :index"
       before_should "find all users" do
         User.expects(:find).with(:all).returns(@users)
       end
     end
   end
 end
     # File lib/shoulda/context.rb, line 128
128:     def before_should(name, &blk)
129:       should(name, :before => blk) { assert true }
130:     end
context(name, &blk) click to toggle source

Contexts

A context block groups should statements under a common set of setup/teardown methods. Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability and readability of your test code.

A context block can contain setup, should, should_eventually, and teardown blocks.

 class UserTest < Test::Unit::TestCase
   context "A User instance" do
     setup do
       @user = User.find(:first)
     end

     should "return its full name"
       assert_equal 'John Doe', @user.full_name
     end
   end
 end

This code will produce the method "test: A User instance should return its full name. ".

Contexts may be nested. Nested contexts run their setup blocks from out to in before each should statement. They then run their teardown blocks from in to out after each should statement.

 class UserTest < Test::Unit::TestCase
   context "A User instance" do
     setup do
       @user = User.find(:first)
     end

     should "return its full name"
       assert_equal 'John Doe', @user.full_name
     end

     context "with a profile" do
       setup do
         @user.profile = Profile.find(:first)
       end

       should "return true when sent :has_profile?"
         assert @user.has_profile?
       end
     end
   end
 end

This code will produce the following methods

  • "test: A User instance should return its full name. "

  • "test: A User instance with a profile should return true when sent :has_profile?. "

Just like should statements, a context block can exist next to normal def test_the_old_way; end tests. This means you do not have to fully commit to the context/should syntax in a test file.

     # File lib/shoulda/context.rb, line 195
195:     def context(name, &blk)
196:       if Shoulda.current_context
197:         Shoulda.current_context.context(name, &blk)
198:       else
199:         context = Shoulda::Context.new(name, self, &blk)
200:         context.build
201:       end
202:     end
described_type() click to toggle source

Returns the class being tested, as determined by the test class name.

  class UserTest; described_type; end
  # => User
     # File lib/shoulda/context.rb, line 208
208:     def described_type
209:       self.name.gsub(/Test$/, '').constantize
210:     end
should(name_or_matcher, options = {}, &blk) click to toggle source

Should statements

Should statements are just syntactic sugar over normal Test::Unit test methods. A should block contains all the normal code and assertions you’re used to seeing, with the added benefit that they can be wrapped inside context blocks (see below).

Example:

 class UserTest < Test::Unit::TestCase

   def setup
     @user = User.new("John", "Doe")
   end

   should "return its full name"
     assert_equal 'John Doe', @user.full_name
   end

 end

…will produce the following test:

  • "test: User should return its full name. "

Note: The part before should in the test name is gleamed from the name of the Test::Unit class.

Should statements can also take a Proc as a :before option. This proc runs after any parent context’s setups but before the current context’s setup.

Example:

 context "Some context" do
   setup { puts("I run after the :before proc") }

   should "run a :before proc", :before => lambda { puts("I run before the setup") }  do
     assert true
   end
 end

Should statements can also wrap matchers, making virtually any matcher usable in a macro style. The matcher’s description is used to generate a test name and failure message, and the test will pass if the matcher matches the subject.

Example:

  should validate_presence_of(:first_name).with_message(/gotta be there/)
    # File lib/shoulda/context.rb, line 71
71:     def should(name_or_matcher, options = {}, &blk)
72:       if Shoulda.current_context
73:         Shoulda.current_context.should(name_or_matcher, options, &blk)
74:       else
75:         context_name = self.name.gsub(/Test/, "")
76:         context = Shoulda::Context.new(context_name, self) do
77:           should(name_or_matcher, options, &blk)
78:         end
79:         context.build
80:       end
81:     end
should_eventually(name, options = {}, &blk) click to toggle source

Just like should, but never runs, and instead prints an ‘X’ in the Test::Unit output.

     # File lib/shoulda/context.rb, line 133
133:     def should_eventually(name, options = {}, &blk)
134:       context_name = self.name.gsub(/Test/, "")
135:       context = Shoulda::Context.new(context_name, self) do
136:         should_eventually(name, &blk)
137:       end
138:       context.build
139:     end
should_not(matcher) click to toggle source

Allows negative tests using matchers. The matcher’s description is used to generate a test name and negative failure message, and the test will pass unless the matcher matches the subject.

Example:

  should_not set_the_flash
     # File lib/shoulda/context.rb, line 90
 90:     def should_not(matcher)
 91:       if Shoulda.current_context
 92:         Shoulda.current_context.should_not(matcher)
 93:       else
 94:         context_name = self.name.gsub(/Test/, "")
 95:         context = Shoulda::Context.new(context_name, self) do
 96:           should_not(matcher)
 97:         end
 98:         context.build
 99:       end
100:     end
subject(&block) click to toggle source

Sets the return value of the subject instance method:

  class UserTest < Test::Unit::TestCase
    subject { User.first }

    # uses the existing user
    should validate_uniqueness_of(:email)
  end
     # File lib/shoulda/context.rb, line 220
220:     def subject(&block)
221:       @subject_block = block
222:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.