【问题标题】:Difference in modules when running rspec from "rspec spec" and rspec "spec/services/my/module"从“rspec spec”和rspec“spec/services/my/module”运行rspec时的模块差异
【发布时间】:2015-07-09 10:07:24
【问题描述】:

我的测试:

RSpec.describe Populators::Fipe::Brand, type: :service do
  before(:all) do
    ::Populators::Fipe::Brand.new.perform
  end

  it "update records and do not create new ones" do
    expect{
      ::Populators::Fipe::Brand.new.perform
    }.to_not change{
      ::Brand.maximum(:id)
    }
  end
end

当我跑步时:

rspec 规范

结果:

失败/错误:::Populators::Fipe::Brand.new.perform 无方法错误: '

中未定义的方法perform' for #<Brand:0x007fdcb6fb2788> # ./spec/services/populators/fipe/brand_spec.rb:5:inblock(2 级)

当我跑步时:

rspec spec/services/populators/fipe/brand_spec.rb

结果:

Populators::Fipe::Brand 更新记录,不创建新记录 创建表 填写姓名 填充combo_code

在 1.04 秒内完成(文件加载需要 6.33 秒) 4 个示例,0 次失败

已编辑:

我有两个班级:

models/brand.rb

services/populators/fipe/brand.rb

代码试图做什么:

当 Populators::Fipe::Brand(服务)执行其操作时, 它使用来自网络服务的数据填充品牌(模型)。 但如果品牌(模型)已经存在,它只会更新。

rspec 在做什么:

但似乎 rspec 试图在“models/brand.rb”中调用方法“perform” 而不是正确的“services/populators/fipe/brand.rb”。 不知道为什么..

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    首先,您可以将::Populators::Fipe::Brand 替换为described_class,其次,:: 表示可能强制使用最上面的上下文。你的错误只是说Brand 类没有一些方法,我认为你在这里写的是::Populators::Fipe::Brand 而不是::Brand。尝试将您的代码更改为

    RSpec.describe Populators::Fipe::Brand, type: :service do
      before(:all) do
        described_class.new.perform
      end
    
      it "update records and do not create new ones" do
        expect{
          described_class.new.perform
        }.to_not change{
          described_class.maximum(:id)
        }
      end
    end
    

    【讨论】:

    • Piotr,感谢“描述类”,我将在未来的规范中使用,但错误仍然存​​在。我编辑了问题的解释,也许你有其他的见解。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多