35

I'm trying to check that a new action in my RESTful controller set up an instance variable of the required Object type. Seems pretty typical, but having trouble executing it

Client Controller

def new
  @client = Client.new
end  

Test

describe "GET 'new'" do
  it "should be successful" do
    get 'new'
    response.should be_success
  end

  it "should create a new client" do
    get 'new'
    assigns(:client).should == Client.new
  end
end

Results in...

'ClientsController GET 'new' should create a new client' FAILED
  expected: #,
       got: # (using ==)

Which is probably because it's trying to compare 2 instances of active record, which differ. So, how do I check that the controller set up an instance variable that holds a new instance of the Client model.

0

3 Answers 3

37

technicalpickles in #rspec helped me out...

assigns(:client).should be_kind_of(Client)
Sign up to request clarification or add additional context in comments.

3 Comments

How about if Im working with a REST and I don't use "@" in the controllers? Is there a workaround? Thanks
For rails 5, use expect(assigns(:client).to be_a(Client)
What to do for something like @roles = UserRole.all
16

You can use Rails' new_record? method to check if a given instance has been saved to the database already or not:

assigns(:client).should be_a(Client)
assigns(:client).should be_new_record

(You can't use assigns[:client] for historical reasons, according to: http://guides.rubyonrails.org/testing.html#the-four-hashes-of-the-apocalypse)

1 Comment

The link to "four-hashes-of-the-apocalypse" explains everything a Rails dev needs to know about the environment views receive from controllers!
15

For that 2016 RSpec v3 syntax use:

expect(assigns(:client)).to be_a(Client)

Also, for others that may stop here because of the question title, here is how to test if an instance variable @client was set to anything

# in controller action
@client = Client.find_by(name: "John")

# spec assignment based on collection membership

expect(assigns.keys).to include('client')

See @Attila Györffy's answer about why the assigns hash must be accessed with string keys.

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.