Rspec: NoMethodError

144 Views Asked by At

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

1

There are 1 best solutions below

0
On

You need to create a scope on the Shelf class to actually search for enabled records.

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

  ...
end