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

**URL:** <https://www.ruby-forum.com/t/why-is-my-rspec-controller-test-for-get-method-not-working/237514>\
**Category:** Rails\
**Created:** [November 17, 2013, 7:10am UTC](https://www.ruby-forum.com/t/why-is-my-rspec-controller-test-for-get-method-not-working/237514 "2013-11-17T07:10:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [November 17, 2013, 7:10am UTC](https://www.ruby-forum.com/t/why-is-my-rspec-controller-test-for-get-method-not-working/237514/1 "2013-11-17T07:10:34Z")

</div>

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](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 H.'s, Pragmatic Studio, and Code School.

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

p

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 17, 2013, 8:52am UTC](https://www.ruby-forum.com/t/why-is-my-rspec-controller-test-for-get-method-not-working/237514/2 "2013-11-17T08:52:11Z")

</div>

[https://github.com/haloflightleader/bank/commit/88762271b5feda1390054b472cfb1de006c2b5da](https://github.com/haloflightleader/bank/commit/88762271b5feda1390054b472cfb1de006c2b5da)

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

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 17, 2013, 4:00pm UTC](https://www.ruby-forum.com/t/why-is-my-rspec-controller-test-for-get-method-not-working/237514/3 "2013-11-17T16:00:22Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [November 21, 2013, 3:34am UTC](https://www.ruby-forum.com/t/why-is-my-rspec-controller-test-for-get-method-not-working/237514/4 "2013-11-21T03:34:11Z")

</div>

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.
