如何“筑巢”?或“组”测试::单元测试?
RSpec 有:
describe "the user" do
before(:each) do
@user = Factory :user
end
it "should have access" do
@user.should ...
end
end
您如何将这样的测试与 Test::Unit 分组?例如,在我的控制器测试中,我想在用户登录和无人登录时测试控制器。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过课程实现类似的目标。可能有人会说这太可怕了,但它确实允许您在一个文件中分离测试:
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:
据我所知,
Test::Unit
不支持测试上下文。但是,gemcontest
添加了对上下文块的支持。Test::Unit
, to my knowledge, does not support test contexts. However, the gemcontest
adds support for context blocks.应该 https://github.com/thoughtbot/shoulda 虽然看起来他们现在已经创建了上下文- 相关代码放入单独的 gem 中: https://github.com/thoughtbot/shoulda-context
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
使用
shoulda-context
:在您的 Gemfile 中:
以及在您的测试中您可以执行以下操作的文件(请注意
should
而不是test
:Using
shoulda-context
:In your Gemfile:
And in your test files you can do things like (notice the
should
instead oftest
: