我是测试 Rails 网络应用程序和 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 }
其中 should_match_find_method
自定义辅助方法
最佳答案
RSpec 的创建者最近在博客上发表了他认为 Validations are behavior, associations are structure .换句话说,他发现关联(和范围)不应该被直接测试。将根据您想要的行为对这些进行测试。
换句话说,目前的观点是没有必要直接测试每个范围,因为您将通过测试应用程序的行为来覆盖这些关联。
关于ruby-on-rails - rails : Testing named scopes with RSpec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485379/