RSpec 有:
describe "the user" do
before(:each) do
@user = Factory :user
end
it "should have access" do
@user.should ...
end
end
你会如何用 Test::Unit 对这样的测试进行分组?例如,在我的控制器测试中,我想在用户登录和无人登录时测试控制器。
RSpec 有:
describe "the user" do
before(:each) do
@user = Factory :user
end
it "should have access" do
@user.should ...
end
end
你会如何用 Test::Unit 对这样的测试进行分组?例如,在我的控制器测试中,我想在用户登录和无人登录时测试控制器。
您可以通过类实现类似的目标。可能有人会说这很可怕,但它确实允许您在一个文件中分离测试:
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
,据我所知,不支持测试上下文。但是,gemcontest
添加了对上下文块的支持。
应该https://github.com/thoughtbot/shoulda虽然看起来他们现在已经将与上下文相关的代码变成了一个单独的 gem:https ://github.com/thoughtbot/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