【发布时间】:2011-09-23 01:04:01
【问题描述】:
我是测试 Rails Web 应用程序和 RSpec 的新手。我使用遗留代码,需要添加测试。那么使用 RSpec 测试查找器和命名范围的最佳方法是什么?
我在 Google 中找到了一些方法,但它们并不理想。 例如:
http://paulsturgess.co.uk/articles/show/93-using-rspec-to-test-a-named_scope-in-ruby-on-rails
it "excludes users that are not active" do
@user = Factory(:user, :active => false)
User.active.should_not include(@user)
end
或
http://h1labs.com/notebook/2008/8/21/testing-named-scope-with-rspec
it "should have a published named scope that returns ..." do
Post.published.proxy_options.should == {:conditions => {:published => true}}
end
我在“Rail Test Prescriptions”中找到了最佳方法(恕我直言):
should_match_find_method :active_only { :active == true }
where should_match_find_method 自定义辅助方法
【问题讨论】:
-
范围测试真的是经验性的,我坚持你在这里公开的第一种方法
-
我同意。第一种方法有什么问题?它规范了真实的行为,而不是检查(实际上只是重写)可能有缺陷的配置参数。
-
@RobDavis “可能有缺陷的配置参数”+1
标签: ruby-on-rails ruby testing rspec