ruby-on-rails - rails : Testing named scopes with RSpec

标签 ruby-on-rails ruby testing rspec

我是测试 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/

相关文章:

testing - Cypress - 在点击后记录来自请求的响应数据()

Ruby:模块、要求和包含

ruby-on-rails - Rails STI 和多级继承查询

ruby-on-rails - 在 rails gem 上测试不同的数据库后端

ruby-on-rails - Rails Controller 中的持久参数

node.js - 从未编写过任何自动化测试,我应该如何开始行为驱动开发?

javascript - 我应该使用什么测试方法?

html - 为 .each 循环中的一个条目显示带有表显示值的模态

ruby-on-rails - <a> 中的 Ruby on Rails 链接标记不起作用

ruby-on-rails - 如何在表单collection_select rails中将多个id作为数组传递?