开发者

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

开发者 https://www.devze.com 2023-03-04 20:09 出处:网络
RSpec has: describe \"the user\" do before(:each) do @user = 开发者_运维知识库Factory :user end it \"should have access\" do

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.


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


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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消