我是测试Rails web应用程序和RSpec的新手.我使用遗留代码,需要添加测试.那么,用RSpec测试查找程序和命名作用域的最佳方法是什么呢?

我在谷歌找到了一些方法,但并不理想.

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 = Fact或y(: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

我在"铁路测试处方"中找到了最佳方法(IMHO):

should_match_find_method :active_only { :active == true }

其中should_match_find_method个自定义助手方法

推荐答案

RSpec的创始人最近在博客上写道,他认为自己是Validations are behavior, associations are structure岁.换句话说,他发现不必直接测试关联(和范围).这些测试将根据您想要的行为进行.

换句话说,目前的看法是,不需要直接测试每个范围,因为您将通过测试应用程序的行为来覆盖这些关联.

Ruby-on-rails相关问答推荐

form_with正在try 用错误的路径生成操作

Grape api (rails) - 未初始化常量 Endpoints::TodoAPI (NameError)

Rails 7,Hotwire 动态表单,更新值不起作用

Rails 7 共享刺激控制器功能

Ruby 3 动态关键字参数

Ruby on rails 查询不知道怎么写

当我必须更改 rspec 中的方法时,如何检测时间?

Rails 3 应用程序的 MySQL 集群 (NDB) 与 MySQL 复制 (InnoDB):优点/缺点?

在 Rails 的 ActiveRecord 中,touch 有什么用?

我如何存根 find_each 以在 rails 3 中进行 rspec 测试

如何将 yaml 文件解析为 ruby​​ 哈希和/或数组?

带有 master.key 的 Rails 5.2 - Heroku 部署

在 PostgreSQL 中强制字符串的最大长度

关于在不适合任何地方的 Rails 应用程序中放置类的指南

如何检测导致 Rake 中的弃用警告的原因

设计:在注册期间禁用密码确认

是否可以设置 travis 来运行多种语言的测试?

Rails 3 设计,current_user 在模型中不可访问?

即使X-Frame-Options为ALLOWALL,也无法在 iframe 中显示我的 rails 4 应用程序

如何从 ApplicationHelper 调用 ApplicationController 方法