首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用has_many测试肩匹配器rspec3

用has_many测试肩匹配器rspec3
EN

Stack Overflow用户
提问于 2014-10-02 18:09:03
回答 2查看 1.9K关注 0票数 2

我第一次尝试用一个新的应用程序使用rspec3。我定义了两个简单的模型:

代码语言:javascript
运行
复制
class Match < ActiveRecord::Base
end

class Article < ActiveRecord::Base
  has_many :matches
end

当我从控制台访问这些模型时,我可以创建关联并按预期将它们存储在DB中。但是,当我试图运行一个规范来测试这一点时,它似乎不起作用:

代码语言:javascript
运行
复制
require 'spec_helper'

describe Article do
   it { is_expected.to have_many(:matches) }
   # it { should have_many(:matches) }
end

我尝试同时运行测试的isexpected版本和should版本,但在这两种情况下,我都得到了以下错误:

代码语言:javascript
运行
复制
> ./bin/rspec spec/models/article_spec.rb
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
F

Failures:

  1) Article should have many :matches
     Failure/Error: it { is_expected.to have_many :matches }
       expected #<Article:0x007fa14c276f38> to respond to `has_many?`
     # ./spec/models/article_spec.rb:4:in `block (2 levels) in <top (required)>'
     # -e:1:in `<main>'

Finished in 0.09484 seconds (files took 0.63853 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/article_spec.rb:4 # Article should have many :matches

Randomized with seed 24636

下面是Gemfile.lock中的相关依赖项:

代码语言:javascript
运行
复制
rspec-core (3.0.4)
  rspec-support (~> 3.0.0)
rspec-expectations (3.0.4)
  rspec-support (~> 3.0.0)
rspec-mocks (3.0.4)
  rspec-support (~> 3.0.0)
rspec-rails (3.0.2)
  rspec-core (~> 3.0.0)
  rspec-expectations (~> 3.0.0)
  rspec-mocks (~> 3.0.0)
  rspec-support (~> 3.0.0)
rspec-support (3.0.4)
spring-commands-rspec (1.0.2)
rspec-rails (~> 3.0.0)
spring-commands-rspec
...
  shoulda-matchers (2.7.0)
shoulda-matchers

看起来应该很直截了当。我是不是做错了什么导致我犯了这个错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-02 18:57:38

这是由于运行在安装Spring时创建的binstub:

代码语言:javascript
运行
复制
./bin/rspec spec/models/article_spec.rb

显然,这与运行RSpec安装的可执行文件的方式不同,这只是:

代码语言:javascript
运行
复制
rspec spec/models/article_spec.rb
票数 2
EN

Stack Overflow用户

发布于 2015-03-03 20:35:09

我不知道根本原因是什么,但我只是遇到了同样的问题-- bin/rspec和rspec。我设法让它为rspec工作,但没有为bin/rspec工作。

我必须将require 'shoulda/matchers'添加到规范文件中,还必须添加rails_helper.rb才能通过测试。它通过了一次,之后我可以从spec文件中删除它,然后测试就通过了。奇怪和不满意的解决办法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26166851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档