首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rspec: NoMethodError

Rspec: NoMethodError
EN

Stack Overflow用户
提问于 2014-08-19 17:45:05
回答 1查看 127关注 0票数 0

我正在为模型编写测试用例(单元测试),并且得到了一个错误,我真的不知道为什么……

下面是我的错误:

代码语言:javascript
复制
Failure/Error: expect(Shelf.enabled.count).to eq 2
     NoMethodError:
       undefined method `enabled' for #<Class:0x00000005a2c088>

这是我在模型规范中的代码。models/shelf_spec.rb

代码语言:javascript
复制
describe 'shelves' do
    before do
      Fabricate(:shelf, heading_products: 'Top Selling Products', heading_vendors: 'Top Selling Brands', enabled: true, product_ids: '1, 2', vendor_ids: '1, 3')
      Fabricate(:shelf, enabled: true, expires_on: Date.today)
      Fabricate(:shelf, enabled: false, expires_on: 1.day.ago)
    end
describe 'Shelf#enabled' do
      it 'should return enabled shelves' do
        expect(Shelf.enabled.count).to eq 2
      end

      it 'shelves returned should be enabled' do
        expect(Shelf.enabled.first.enabled?).to be_true
      end
    end
end

enabled是工具架布尔类型的属性。

请纠正我遗漏或错误的地方。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-08-19 21:25:29

您需要在Shelf类上创建一个作用域,以便实际搜索已启用的记录。

代码语言:javascript
复制
class Shelf
  scope :enabled, -> { where(enabled: true) }

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

https://stackoverflow.com/questions/25380300

复制
相关文章

相似问题

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