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

Hello Everyone,

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

https://github.com/haloflightleader/bank/blob/02695c7c5106b30e178d3a0d05125f8deedd5e67/spec/controllers/accounts_controller_spec.rb#L18

This is practically a copy from the rails guides and it works in my index test. The render test works… it’s only the assigns portion that refuses 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 Hartl’s, Pragmatic Studio, and Code School.

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

p

https://github.com/haloflightleader/bank/commit/88762271b5feda1390054b472cfb1de006c2b5da

I corrected my code, but am still getting this:

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 you 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 illustrate 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 be 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 it was a hash in the index action because I gave it a hash. But the way it is being used in my new action, it’s not a hash, it’s just a new Account object that is empty. Thank you.