Question

How do you "nest" or "group" Test::Unit tests?

RSpec has:

describe "the user" do
  before(:each) do
    @user = Factory :user
  end

  it "should have access" do
    @user.should ...
  end
end

How would you group tests like that with Test::Unit? For example, in my controller test, I want to test the controller when a user is signed in and when nobody is signed in.

 21  7560  21
1 Jan 1970

Solution

 11

You can achieve something similar through classes. Probably someone will say this is horrible but it does allow you to separate tests within one file:

class MySuperTest < ActiveSupport::TestCase
  test "something general" do
    assert true
  end

  class MyMethodTests < ActiveSupport::TestCase

    setup do
      @variable = something
    end

    test "my method" do
      assert object.my_method
    end
  end
end
2013-08-07

Solution

 6

Test::Unit, to my knowledge, does not support test contexts. However, the gem contest adds support for context blocks.

2011-05-08