Roll your own shoulda macros

Angelegt von suung Tue, 10 Aug 2010 18:50:00 GMT

I wanted a good tutorial and I found it:

 

excerpt:

class Test::Unit::TestCase
  def self.should_have_attr_reader name
    klass = self.name.gsub(/Test$/, '').constantize
    should "have attr_reader :#{name}" do
      obj = klass.new
      obj.instance_variable_set("@#{name}", "SomeSecretValue")
      assert_equal("SomeSecretValue", obj.send(name))
    end
  end
end

# Usage
class PostTest
  should_have_attr_reader :comments_number
end

Cascaded Contexts in Ruby - Tests

Angelegt von suung Wed, 28 Jul 2010 15:08:00 GMT

There is Shoulda which does an excellent job.

But for people with less need for cool features, this might be good:

  GNU nano 2.2.4                                                      File: README.markdown                                                                                                                    

          @value += 1
        end

        should "equal 2" do
          assert_equal 2, @value
        end
      end
    end

You can run it normally, it's Test::Unit after all. If you want to run a particular test, say "yet more tests", try this:

    $ testrb my_test.rb -n test_yet_more_tests

Or with a regular expression:

    $ testrb my_test.rb -n /yet_more_tests/

Installation
------------

    $ sudo gem install contest

If you want to use it with Rails, add this to config/environment.rb:

    config.gem "contest"

Then you can vendor the gem:

    rake gems:install
    rake gems:unpack