Module | Shoulda::Macros |
In: |
lib/shoulda/macros.rb
|
Macro that creates a test asserting a change between the return value of a block that is run before and after the current setup block is run. This is similar to Active Support‘s assert_difference assertion, but supports more than just numeric values. See also should_not_change.
The passed description will be used when generating the test name and failure messages.
Example:
context "Creating a post" do setup { Post.create } should_change("the number of posts", :by => 1) { Post.count } end
As shown in this example, the :by option expects a numeric difference between the before and after values of the expression. You may also specify :from and :to options:
should_change("the number of posts", :from => 0, :to => 1) { Post.count } should_change("the post title", :from => "old", :to => "new") { @post.title }
Combinations of :by, :from, and :to are allowed:
# Assert the value changed in some way: should_change("the post title") { @post.title } # Assert the value changed to anything other than "old:" should_change("the post title", :from => "old") { @post.title } # Assert the value changed to "new:" should_change("the post title", :to => "new") { @post.title }
# File lib/shoulda/macros.rb, line 37 37: def should_change(description, options = {}, &block) 38: by, from, to = get_options!([options], :by, :from, :to) 39: stmt = "change #{description}" 40: stmt << " from #{from.inspect}" if from 41: stmt << " to #{to.inspect}" if to 42: stmt << " by #{by.inspect}" if by 43: 44: if block_given? 45: code = block 46: else 47: warn "[DEPRECATION] should_change(expression, options) is deprecated. " << 48: "Use should_change(description, options) { code } instead." 49: code = lambda { eval(description) } 50: end 51: before = lambda { @_before_should_change = code.bind(self).call } 52: should stmt, :before => before do 53: old_value = @_before_should_change 54: new_value = code.bind(self).call 55: assert_operator from, :===, old_value, "#{description} did not originally match #{from.inspect}" if from 56: assert_not_equal old_value, new_value, "#{description} did not change" unless by == 0 57: assert_operator to, :===, new_value, "#{description} was not changed to match #{to.inspect}" if to 58: assert_equal old_value + by, new_value if by 59: end 60: end
Macro that creates a test asserting that a record of the given class was created.
Example:
context "creating a post" do setup { Post.create(post_attributes) } should_create :post end
# File lib/shoulda/macros.rb, line 98 98: def should_create(class_name) 99: should_change_record_count_of(class_name, 1, 'create') 100: end
Macro that creates a test asserting that a record of the given class was destroyed.
Example:
context "destroying a post" do setup { Post.first.destroy } should_destroy :post end
# File lib/shoulda/macros.rb, line 111 111: def should_destroy(class_name) 112: should_change_record_count_of(class_name, -1, 'destroy') 113: end
Macro that creates a test asserting no change between the return value of a block that is run before and after the current setup block is run. This is the logical opposite of should_change.
The passed description will be used when generating the test name and failure message.
Example:
context "Updating a post" do setup { @post.update_attributes(:title => "new") } should_not_change("the number of posts") { Post.count } end
# File lib/shoulda/macros.rb, line 74 74: def should_not_change(description, &block) 75: if block_given? 76: code = block 77: else 78: warn "[DEPRECATION] should_not_change(expression) is deprecated. " << 79: "Use should_not_change(description) { code } instead." 80: code = lambda { eval(description) } 81: end 82: before = lambda { @_before_should_not_change = code.bind(self).call } 83: should "not change #{description}", :before => before do 84: new_value = code.bind(self).call 85: assert_equal @_before_should_not_change, new_value, "#{description} changed" 86: end 87: end