RSpec:#<RSpec::Core::ExampleGroup::Nested_1:0x007fcc2f626d50> 的未定义方法“double”

Posted

技术标签:

【中文标题】RSpec:#<RSpec::Core::ExampleGroup::Nested_1:0x007fcc2f626d50> 的未定义方法“double”【英文标题】:RSpec: undefined method `double' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcc2f626d50> 【发布时间】:2012-02-02 22:05:03 【问题描述】:

我写了一个简单的测试,如下:

    require 'spec_helper.rb'

describe Channel do
  before(:each) do
    @channel = Channel.new
  end

  it "should get the true view count" do 
    upload_view_count = double('upload view count')
    upload_view_count.should_receive(:upload_num).and_return(16000666)
    @channel.upload_view_counts << upload_view_count
    @channel.save()
    @channel.true_all_time_views.should equal(16000666)
  end

  it "should get the true view count with multiple upload view counts" do
    upload_vc1 = double('uplaod view count 1')
    upload_vc1.should_receive(:created_at).and_return(Time.now())
    upload_vc1.should_receive(:upload_num).and_return(17666)
    upload_vc1.should_receive(:updated_at).and_return(Time.now())

    upload_vc2 = double('upload view count 2')
    upload_vc2.should_receive(:created_at).and_return(Time.now())
    upload_vc2.should_receive(:upload_num).and_return(17777)
    upload_vc2.should_receive(:updated_at).and_return(Time.now())

    @channel.upload_view_counts << upload_vc1
    @channel.upload_view_counts << upload_vc2
    @channel.save()
    @channel.true_all_time_views.should equal(17777)
  end




end

当我尝试运行此测试时,我收到以下错误:

失败:

1) 频道应该获得真实的观看次数 失败/错误:upload_view_count = double('upload view count') 无方法错误: '

中未定义的方法double' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcc2f66a8c0> # ./spec/models/channel_spec.rb:9:inblock(2 级)

2) 频道应该通过多次上传视图获得真实的观看次数 计数 失败/错误:upload_vc1 = double('uplaod view count 1') 无方法错误: '

中未定义的方法double' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fcc2f626d50> # ./spec/models/channel_spec.rb:17:inblock(2 级)

在 37.68 秒内完成 5 个示例,2 个失败,3 个待处理

失败的例子:

rspec ./spec/models/channel_spec.rb:8 # Channel 应该得到 true view count rspec ./spec/models/channel_spec.rb:16 # Channel 应该得到 具有多个上传观看次数的真实观看次数

我不知道为什么 double() 方法不起作用。我已经搜索了这个特定错误的高低,我看到的最接近相关的东西是缺少需要'spec_helper.rb',但我有那行。有什么想法吗?

【问题讨论】:

【参考方案1】:

最终导致 config.mock_with :mocha 行错误地包含在我的 spec_helper.rb 文件中。删除它就可以了。

【讨论】:

以上是关于RSpec:#<RSpec::Core::ExampleGroup::Nested_1:0x007fcc2f626d50> 的未定义方法“double”的主要内容,如果未能解决你的问题,请参考以下文章

如何在 RSpec (rails) 中创建或指定关联?

用导轨上的文件或图像进行Rspec的正确方法是什么?

Rspec 不显示邮件正文中的内容

有没有办法用 Rspec 存根包含模块的方法?

如何在 rspec 中使用 capybara 点击下拉选项

RSpec Stubbing:返回参数