0

I am writing test cases (Unit testing) for models, and getting an error which i really don't know why...

here is my error:

Failure/Error: expect(Shelf.enabled.count).to eq 2
     NoMethodError:
       undefined method `enabled' for #<Class:0x00000005a2c088>

and here is my code within model specs. models/shelf_spec.rb

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 is attribute of shelf Boolean type.

please correct me what i am missing or wrong with.

Thanks

4

1 回答 1

1

您需要在Shelf类上创建一个范围以实际搜索启用的记录。

class Shelf
  scope :enabled, -> { where(enabled: true) }

  ...
end
于 2014-08-19T13:25:29.137 回答