Why is my rspec controller test for get method not working?

Hello Everyone,

Why is my controller test in the get method not working?

This is practically a copy from the rails guides and it works in my
test. The render test works… it’s only the assigns portion that
to work.

I’m studying Rails, but am focusing on rspec because Rails by itself is
easy and I feel that I don’t want to write anything without rspec being
right next to it, even if I have to write the tests after. I have gone
through several tutorials already:
Michael H.'s, Pragmatic Studio, and Code School.

So now, I just need to do it and gain proficiency. Thank you.



I corrected my code, but am still getting this:

18 examples, 1 failure
Failed examples:
rspec ./spec/controllers/accounts_controller_spec.rb:18 #
AccountsController Get new assigned @account
Randomized with seed 52138

I would have to contest that you didn’t quite correct it then, eh? :stuck_out_tongue:

You wrote an expectation that says “I expect @account to be an array of
accounts that contains a single, specific instance of Account.” Why are
looking for an array? Your controller code looks right - it assigns
@account to be a new instance Account.

So you, you have two problems here. The one I described you can
this way:

expected = [account]
puts “Expected class: #{expected.class}”
puts “Actual class: #{assigns(:account).class}”
expect( assigns(:account) ).to eq( expected )

The second is that you’re comparing two new instances of Account, which
will not be equal.

puts Account.new == Account.new

You probably just want to check what type of object was assigned to
@account and that it is unpersisted. Something like (and my syntax might
off here):

actual = assigns(:account)
expect(actual).to be_a(Account)
expect(actual).to be_new_record

Thanks for that. I didn’t know there was such a ‘be_a()’ matcher. Where
could I read more about the available permutations of ‘be_’? Thank you.

Also, I read again the Rails Guides on The Four Hashes of the
Apocalypse. I
thought assigns() would be a hash no matter what. I understand now that
was a hash in the index action because I gave it a hash. But the way it
being used in my new action, it’s not a hash, it’s just a new Account
object that is empty. Thank you.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs