1

I'm testing to make sure that a created user is assigned to my instance variable @user. I understand what get means, but I'm not sure what to write for the test. I'm returning with an argument error for a bad URI or URL. What's wrong with my test and how do I fix it?

it "checks @user variable assignment for creation" do

    p = FactoryGirl.create(:user)
    get :users
    # I'm confused on what this line above means/does. What does the hash :users refer 
    #to
    assigns[:user].should == [p]

end

The expected URI object or string error refers to get :users and the error is as follows

Failure/Error get :users
ArgumentError:
  bad argument: (expected URI object or URI string)

2 Answers 2

1

I guess that what you want is

it "checks @user variable assignment for creation" do
    p = FactoryGirl.create(:user)
    get :show, id: p.id
    assigns(:user).should == p
end

The line you were not sure about checks that content of the assigned variable (@user) in the show view of the user p, is equal to the p user you just created more information there

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

7 Comments

It's still giving me an URI error. Just to clarify, I want to check that the user I created in the users/new transfers its form info into the variable @user in the users/creation controller.
I'm not sure I entirely understood what you want to test, and the error you're getting is logical, the correct params you can give to get are either :index without any params, or :edit & :show with the :id params as I shown in my answer
Let me ask you this. What does get actually do? I think that will help me solve this. As for my test, this is what I'm trying to replicate youtube.com/watch?v=05WQlbaRtm8&feature=relmfu at 1:02:00
get will trigger a get request on the controller you're currently testing, and the action it will target is the first parameter you're passing to the function, more info there : api.rubyonrails.org/classes/ActionController/TestCase.html
which part of the video are you trying to replicate ? ( I don't want to watch one hour of video ;) )
|
0

what action are you trying to test? usually, for creation, you need to test that the controller's "create" action creates a user and assigns an @user variable

I would test it this way:

describe 'POST create' do
  it 'creates a user' do
    params = {:user => {:name => 'xxx', :lastname => 'yyy'}}
    User.should_receive(:create).with(params)
    post :create
  end

  it 'assigns the user to an @user instance variable' do
    user = mock(:user)
    User.stub!(:create => user)
    post :create
    assigns(:user).should == user
  end
end

notice that I stub/mock all user methods, since you are testing a controller you don't have to really create the user, you only test that the controller calls the desired method, the user creation is tested inside the User model spec

also, I made 2 tests (you should test only 1 thing on each it block if possible, first it test that the controller creates a user, then I test that the controller assigns the variable

I'm assuming your controller is something like this:

controller...
  def create
    @user = User.create(params[:user])
  end

which is TOO simple, I guess you have more code and you should test that code too (validations, redirects, flash messages, etc)

Comments

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.