Rail controller with rspec

I am new to rails and I am trying to test a controller using rspec. My first test is when the show action is called, it should search for a category by URL.

The problem is that when I add the stubbing code, I get the following error:

undefined `find 'method for #

my test is as follows:

require 'spec_helper' describe CategoriesController do describe "GET /category-name/" do before(:each) do @category = mock_model(Category) Category.stub!(:find).with(:first, :conditions => ["url = :url", {:url => "category-name"}]).and_return(@category) end it "should find the category by url" do controller.show Category.should_receive(:find).with(:first, :conditions => ["url = :url", {:url => "category-name"}]).and_return(@category) end end end 
+4
source share
1 answer

Your request for a request should be after any should_receive . This is a tense thing. So it kind of reads like this: "The category should get something when this happens." "This is happening" refers to the request.

 it "should find the category by url" do Category.should_receive(:find).with... get "show", { your params, if you're sending some in } end 

In addition, you want to go to the request method, and not the controller method itself, for this particular test.

So

 post "action_name" get "action_name" update "action_name" delete "action_name" 

instead

 controller.action_name 
+10
source

Source: https://habr.com/ru/post/1302691/


All Articles