21

RSpec 有:

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

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

你会如何用 Test::Unit 对这样的测试进行分组?例如,在我的控制器测试中,我想在用户登录和无人登录时测试控制器。

4

4 回答 4

11

您可以通过类实现类似的目标。可能有人会说这很可怕,但它确实允许您在一个文件中分离测试:

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-07T12:30:19.453 回答
6

Test::Unit,据我所知,不支持测试上下文。但是,gemcontest添加了对上下文块的支持。

于 2011-05-08T00:17:58.897 回答
3

应该https://github.com/thoughtbot/shoulda虽然看起来他们现在已经将与上下文相关的代码变成了一个单独的 gem:https ://github.com/thoughtbot/shoulda-context

于 2011-05-11T11:23:59.823 回答
2

使用shoulda-context

在您的 Gemfile 中:

gem "shoulda-context"

在您的测试文件中,您可以执行以下操作(注意should代替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
于 2017-12-20T17:27:03.950 回答