Module ThoughtBot::Shoulda::General::ClassMethods
In: lib/shoulda/general.rb

Methods

Public Instance methods

Loads all fixture files (test/fixtures/*.yml)

[Source]

    # File lib/shoulda/general.rb, line 12
12:         def load_all_fixtures
13:           all_fixtures = Dir.glob(File.join(Test::Unit::TestCase.fixture_path, "*.yml")).collect do |f|
14:             File.basename(f, '.yml').to_sym
15:           end
16:           fixtures *all_fixtures
17:         end

Macro that creates a test asserting a change between the return value of an expression 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.

Example:

  context "Creating a post"
    setup do
      Post.create
    end

    should_change "Post.count", :by => 1
  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 "Post.count", :from => 0, :to => 1
  should_change "@post.title", :from => "old", :to => "new"

Combinations of :by, :from, and :to are allowed:

  should_change "@post.title"                # => assert the value changed in some way
  should_change "@post.title" :from => "old" # => assert the value changed to anything other than "old"
  should_change "@post.title" :to   => "new" # => assert the value changed from anything other than "new"

[Source]

    # File lib/shoulda/general.rb, line 47
47:         def should_change(expression, options = {})
48:           by, from, to = get_options!([options], :by, :from, :to)
49:           stmt = "change #{expression.inspect}"
50:           stmt << " from #{from.inspect}" if from
51:           stmt << " to #{to.inspect}" if to
52:           stmt << " by #{by.inspect}" if by
53: 
54:           expression_eval = lambda { eval(expression) }
55:           before = lambda { @_before_should_change = expression_eval.bind(self).call }
56:           should stmt, :before => before do
57:             old_value = @_before_should_change
58:             new_value = expression_eval.bind(self).call
59:             assert_operator from, :===, old_value, "#{expression.inspect} did not originally match #{from.inspect}" if from
60:             assert_not_equal old_value, new_value, "#{expression.inspect} did not change" unless by == 0
61:             assert_operator to, :===, new_value, "#{expression.inspect} was not changed to match #{to.inspect}" if to
62:             assert_equal old_value + by, new_value if by
63:           end
64:         end

Macro that creates a test asserting no change between the return value of an expression that is run before and after the current setup block is run. This is the logical opposite of should_change.

Example:

  context "Updating a post"
    setup do
      @post.update_attributes(:title => "new")
    end

    should_not_change "Post.count"
  end

[Source]

    # File lib/shoulda/general.rb, line 79
79:         def should_not_change(expression)
80:           expression_eval = lambda { eval(expression) }
81:           before = lambda { @_before_should_not_change = expression_eval.bind(self).call }
82:           should "not change #{expression.inspect}", :before => before do
83:             new_value = expression_eval.bind(self).call
84:             assert_equal @_before_should_not_change, new_value, "#{expression.inspect} changed"
85:           end
86:         end

[Validate]