0

Having a weird error in spec/controllers/profiles_controller_spec.rb: undefined local variable or method 'profile'

Pasted in relevant sections of the controller:

require 'rails_helper'
require 'factory_girl'

describe ProfilesController, :type => :controller do
  login_user #this is defined elsewhere, not an issue
  describe "PUT update" do
    before(:each) do
      @profile = FactoryGirl.create(:profile)
    end
    context "valid attributes" do
      it "located the requested @profile" do
        put :update, id: @profile, profile: FactoryGirl.attributes_for(:profile)
        assigns(:profile).should eq(@profile)
      end
    end
  end
end
3
  • Can you paste the backtrace? "undefined local variable or method xyz" is a general ruby error when you have a bare word that doesn't reference a defined local variable or method. You don't have any such profile bare word in the spec you posted above, so it's probably coming from something you are calling from this spec. Commented Dec 16, 2014 at 0:04
  • My profile factory: gist.github.com/maclover7/a5a46650a5a59bb4a69d Commented Dec 16, 2014 at 0:07
  • Backtrace: gist.github.com/maclover7/386b4f2a6b7438161096 Commented Dec 16, 2014 at 0:12

2 Answers 2

1

My guess is that there isn't a problem in your spec or your factory. The problem is with the controller you are testing.

The exception you see

1) ProfilesController PUT #update valid attributes located the requested @profile
   Failure/Error: put :update, id: @profile, profile: FactoryGirl.attributes_for(:profile)
   NameError:
     undefined local variable or method `profile' for #<ProfilesController:0x007fae343cfe08>

tells you that your ProfilesController has undefined local variable or method profile.

In your controller, you have this line

if @profile.update(profile)

that most likely causes the error. Perhaps you need need to change that update(profile) to update(profile_params)? This is hard to tell without seeing the whole controller, but that would follow a common pattern. And I guess you have a method profile_params that returns the filtered params.

Sign up to request clarification or add additional context in comments.

1 Comment

0

Have you defined a factory in test/factories.rb or spec/factories.rb?

It'll look something like this:

# This will guess the User class
FactoryGirl.define do
  factory :user do
    first_name "John"
    last_name  "Doe"
    admin false
  end

  # This will use the User class (Admin would have been guessed)
  factory :admin, class: User do
    first_name "Admin"
    last_name  "User"
    admin      true
  end
end

taken directly from the docs

1 Comment

Thanks for replying so quickly! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.