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

Awais
  • 1,803
  • 22
  • 26
  • what for do you use count on boolean? expect(Shelf.enabled.count) – Andrey Deineko Aug 19 '14 at 11:01
  • enabled is Boolean type which i am using to show activate or deactivate the shelf (model). now in my testing i have 3 fabricate(sample shelf), i want to test how are active and how many are not. if i make you understand my requirement ? @andrey – Awais Aug 19 '14 at 11:17
  • Please show your `Shelf` class and its enabled method – phts Aug 19 '14 at 12:11
  • there is no enabled method, enabled is just shelf's attribute. @phts if i need to add method ? because i trying to access direct attribute. – Awais Aug 21 '14 at 14:53

1 Answers1

1

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

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

  ...
end
Paul Elliott
  • 1,174
  • 8
  • 9