Controller test problem

257 views
Skip to first unread message

Roelof Wobben

unread,
Dec 12, 2014, 8:24:40 AM12/12/14
to rs...@googlegroups.com
Hello,

I try to test my controller so I made this :"

require "spec_helper"
describe ProductsController do
   
    describe "GET #index" do
       
        context "with params[:product]" do
           
            it "populates a array containing that product"
                product = create(:product, title: "Book 1")
                get :index , product: "Book 1"
                expect(assigns(:product)).to match_array(["Book 1"])
            end
           
            it "renders the :index view"
    end
   
end

But as soon as I run rspec I see this error message ;

/home/codio/workspace/commerce-try/spec/controllers/products_controller_spec.rb:10:in `block (3 levels) in <top (required)>': undefined method `create' for #<Class:0x007
fa7da640148> (NoMethodError)  

Which I find wierd because on the model test spec I also use create and there no error message

Roelof

Myron Marston

unread,
Dec 12, 2014, 2:27:46 PM12/12/14
to rs...@googlegroups.com
Where is the `create` method defined?  It's not a method provided by rspec or rspec-rails, so there's something else in your environment defining it in model specs but not controller specs.  If you want to see where the method comes from, in your model spec, before the call to `create`, add:
puts method(:create).owner
puts method(:create).source_location
 That should tell you what module has the definition of `create` and what the file and line number is where it is defined.  Based on that, you can include that module in your controller example group to make it available there.

Alternately, consider creating the product record using something like Product.create(...).

HTH,
Myron

Roelof Wobben

unread,
Dec 12, 2014, 2:53:19 PM12/12/14
to rs...@googlegroups.com
The create command is from FactoryGirl.

And on the spec_helper.rb I have this :

config.include FactoryGirl::Syntax::Methods

and on the model testing that was enough.

There I used it like this :

 it "is valid with a productname, description and a image_url " do
            expect(create(:product)).to be_valid
        end

Roelof


Op vrijdag 12 december 2014 20:27:46 UTC+1 schreef Myron Marston:

Myron Marston

unread,
Dec 16, 2014, 3:20:57 AM12/16/14
to rs...@googlegroups.com
On Friday, December 12, 2014 11:53:19 AM UTC-8, Roelof Wobben wrote:
The create command is from FactoryGirl.

And on the spec_helper.rb I have this :

config.include FactoryGirl::Syntax::Methods

and on the model testing that was enough.

There I used it like this :

 it "is valid with a productname, description and a image_url " do
            expect(create(:product)).to be_valid
        end

Roelof

Are you requiring `spec_helper.rb` from your controller spec? 

Roelof Wobben

unread,
Dec 16, 2014, 4:20:12 AM12/16/14
to rs...@googlegroups.com
Yep

Op dinsdag 16 december 2014 09:20:57 UTC+1 schreef Myron Marston:

Ester Ytterbrink

unread,
Dec 16, 2014, 4:50:05 AM12/16/14
to rs...@googlegroups.com
You miss a do after             it "populates a array containing that product", it that just a copy-paste error or the code you are running?
/Ester

Roelof Wobben

unread,
Dec 16, 2014, 8:27:59 AM12/16/14
to rs...@googlegroups.com

Thanks,

I have now this :

require "spec_helper"
describe ProductsController do
   
    describe "GET #index" do
       
        context "with params[:product]", :type => :controller do
           
            it "populates a array containing that product" do

                product = create(:product, title: "Book 1")
                get :index , product: "Book 1"
                expect(assigns(:product)).to match_array(["Book 1"])
            end
           
           
        end
    end
end

But I see this error message :

1) ProductsController GET #index with params[:product] populates a array containing that product                                                                       
     Failure/Error: expect(assigns(:product)).to match_array(["Book 1"])                                                                                                 
       expected a collection that can be converted to an array with `#to_ary` or `#to_a`, but got nil                                                                    
     # ./spec/controllers/products_controller_spec.rb:12:in `block (4 levels) in <top (required)>'   


Roelof


Op dinsdag 16 december 2014 10:50:05 UTC+1 schreef Ester Ytterbrink:

Myron Marston

unread,
Dec 16, 2014, 3:01:01 PM12/16/14
to rs...@googlegroups.com
On Tuesday, December 16, 2014 5:27:59 AM UTC-8, Roelof Wobben wrote:

Thanks,

I have now this :

require "spec_helper"
describe ProductsController do
   
    describe "GET #index" do
       
        context "with params[:product]", :type => :controller do
           
            it "populates a array containing that product" do
                product = create(:product, title: "Book 1")
                get :index , product: "Book 1"
                expect(assigns(:product)).to match_array(["Book 1"])
            end
           
           
        end
    end
end

But I see this error message :

1) ProductsController GET #index with params[:product] populates a array containing that product                                                                       
     Failure/Error: expect(assigns(:product)).to match_array(["Book 1"])                                                                                                 
       expected a collection that can be converted to an array with `#to_ary` or `#to_a`, but got nil                                                                    
     # ./spec/controllers/products_controller_spec.rb:12:in `block (4 levels) in <top (required)>'   


Roelof

The failure message indicates that `assigns(:product)` returned `nil`, and thus can't be converted to an array for comparison with `["Book 1"]`.

We're happy to help here on the mailing list with general questions about how RSpec works and how to use it well, but we don't have the capacity to help you through each step of building and testing your application.  If you need more in-depth help with your application, I'm available on code mentor for paid sessions:


Reply all
Reply to author
Forward
0 new messages