Reputation: 155
I want to test whether a method in my controller has been called or not.
My Controller looks like this:
def index
if id_filters
@products = Product.where(id: id_filters)
else
@products = Product.paginate(params[:page].to_i, params[:per].to_i)
end
render json: @products, meta: @products.meta
end
I saw people using the code below to do this so I attempted to test it in RSpec with:
controller.stub!(:paginate).and_return true
however, I get an error:
undefined method `stub!' for #<ProductsController:0x00000102bd4d38>
I also tried:
controller.stub(:paginate).and_return true
same result though, it is an undefined method.
Upvotes: 0
Views: 84
Reputation: 13181
CORRECT SYNTAX
If you are using rspec before version 3.0 the correct syntax is
controller.should receive(:paginate).and_return(true)
# OR this equivalence
controller.should receive(:paginate) { true }
# OR (when you are in "controller" specs)
should receive(:paginate) { true }
If you are using rspec version 3.0 or higher the correct syntax is
expect(controller).to receive(:paginate) { true }
# OR (when you are in "controller" specs)
is_expected.to receive(:paginate) { true }
YOUR CODE
You seem to be testing paginate
for Product
so you syntax should be:
# Rspec < 3.0
Product.should receive(:paginate) { true }
# Rspec >= 3.0
expect(Product).to receive(:paginate) { true }
Upvotes: 0