| Module | Thoughtbot::Shoulda |
| In: |
lib/shoulda/context.rb
|
| VERSION | = | '2.0.0' |
| contexts | [RW] |
Before statements are simply should statements that run before the current context‘s setup. These are especially useful when setting expectations.
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 do
get :index
end
# normal should statement
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 103
103: def before_should(name, &blk)
104: should(name, :before => blk) { assert true }
105: end
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
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 170
170: def context(name, &blk)
171: if Shoulda.current_context
172: Shoulda.current_context.context(name, &blk)
173: else
174: context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
175: context.build
176: end
177: end
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).
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:
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.
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
# File lib/shoulda/context.rb, line 62
62: def should(name, options = {}, &blk)
63: if Shoulda.current_context
64: block_given? ? Shoulda.current_context.should(name, options, &blk) : Should.current_context.should_eventually(name)
65: else
66: context_name = self.name.gsub(/Test/, "")
67: context = Thoughtbot::Shoulda::Context.new(context_name, self) do
68: block_given? ? should(name, options, &blk) : should_eventually(name)
69: end
70: context.build
71: end
72: end
Just like should, but never runs, and instead prints an ‘X’ in the Test::Unit output.
# File lib/shoulda/context.rb, line 108
108: def should_eventually(name, options = {}, &blk)
109: context_name = self.name.gsub(/Test/, "")
110: context = Thoughtbot::Shoulda::Context.new(context_name, self) do
111: should_eventually(name, &blk)
112: end
113: context.build
114: end