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

4

21

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.

Charbonneau answered 8/5, 2011 at 0:5 Comment(0)
G
6

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

Guff answered 8/5, 2011 at 0:17 Comment(2)
In an attempt to save someone a 15 minutes of debugging, the contest gem is out of date and does not work with my Rails 4 application.Agan
Gem doesn't work on Rails 5.2 either, see github.com/citrusbyte/contest/issues/9Warfourd
B
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
Breadnut answered 7/8, 2013 at 12:30 Comment(0)
G
6

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

Guff answered 8/5, 2011 at 0:17 Comment(2)
In an attempt to save someone a 15 minutes of debugging, the contest gem is out of date and does not work with my Rails 4 application.Agan
Gem doesn't work on Rails 5.2 either, see github.com/citrusbyte/contest/issues/9Warfourd
C
3

Shoulda https://github.com/thoughtbot/shoulda although it looks like they've now made the context-related code into a separate gem: https://github.com/thoughtbot/shoulda-context

Cleveite answered 11/5, 2011 at 11:23 Comment(0)
W
2

Using shoulda-context:

In your Gemfile:

gem "shoulda-context"

And in your test files you can do things like (notice the should instead of test:

class UsersControllerTest < ActionDispatch::IntegrationTest
  context 'Logged out user' do
    should "get current user" do
      get api_current_user_url

      assert_response :success
      assert_equal response.body, "{}"
    end
  end
end
Warfourd answered 20/12, 2017 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.