Forum: RSpec Testing Custom Methods

Posted by Tony Spore (Guest)
on 2011-11-12 23:05
(Received via mailing list)
I am attempting to test a custom method, and totally bombing out.
describe Record do
  describe '#save_cf_data' do
    before  :each do
      @record = mock_model(Record)
    end
    it "should have a birthday" do
      @record.save_cf_data({"final_outputs"=>["birth_day"=>["3"]]})
      @record.birth_day.should eql 3
    end
end

My Model looks like this:
class Record < ActiveRecord::Base
   def save_cf_data(params)
     result = params[:final_outputs].first
       ....
      self.birth_day = result['birth_day'].first
  end
end


I have this output - As if I'm not hitting the method correctly -

Mock "Record_1002" received unexpected message :save_cf_data with
({"final_outputs"=>[{"birth_day"=>["3"]}]})

-Thanks,
Tony Spore
Posted by David Chelimsky (Guest)
on 2011-11-12 23:21
(Received via mailing list)
On Nov 12, 2011, at 3:33 PM, Tony Spore wrote:

> I am attempting to test a custom method, and totally bombing out.
> describe Record do
>   describe '#save_cf_data' do
>     before  :each do
>       @record = mock_model(Record)

mock_model creates a test double, or pure mock ...

>     end
>     it "should have a birthday" do
>       @record.save_cf_data({"final_outputs"=>["birth_day"=>["3"]]})
>       @record.birth_day.should eql 3

... which means that @record here ^^ is a test double, not a Record 
object ...

> end
... which means that this class definition ^^ has nothing to do with 
anything in the example above.

> I have this output - As if I'm not hitting the method correctly -
> Mock "Record_1002" received unexpected message :save_cf_data with 
({"final_outputs"=>[{"birth_day"=>["3"]}]})
>

What you want is either stub_model, or just Record.new or, if you're 
using something like factory_girl, Factory.build(Record).

See https://www.relishapp.com/rspec/rspec-rails/docs/m... 
and https://www.relishapp.com/rspec/rspec-rails/docs/m... 
for more info.

HTH,
David
Posted by Tony Spore (Guest)
on 2011-11-13 02:45
(Received via mailing list)
Great thanks!

So now I am at least calling the method - But when I try to add in the
params that I'm being sent I keep returning nil -
  Failure/Error: @record.save_cf_data(result)
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of Array.
       The error occurred while evaluating nil.delete

In the above case I create the hash as result
= {"final_outputs"=>["surname"=>["hagan"]]}

When I manually place in the params -
Failure/Error:
@record.save_cf_data({"final_outputs"=>["surname"=>["hagan"]]})
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of Array.
       The error occurred while evaluating nil.first

So now as I go down this path, I would say that I am not feeding into 
the
method my params. How would it be best to do that?

I am right now just trying to use rspec with no additional params.

Thanks again!





-Thanks,
Tony Spore
Posted by David Chelimsky (Guest)
on 2011-11-13 17:15
(Received via mailing list)
On Nov 12, 2011, at 6:27 PM, Tony Spore wrote:

> On Nov 12, 2011, at 3:33 PM, Tony Spore wrote:
>>     it "should have a birthday" do
>>    def save_cf_data(params)
>>
>
> What you want is either stub_model, or just Record.new or, if you're using 
something like factory_girl, Factory.build(Record).
>
> See https://www.relishapp.com/rspec/rspec-rails/docs/m... and 
https://www.relishapp.com/rspec/rspec-rails/docs/m... for more info.
>
> HTH,
> David

>
>
> Thanks again!

I'd love to help, but ...

1. please post either inline or at the bottom (I moved your post to the 
bottom in this case - see http://idallen.com/topposting.html).
2. please post actual code or a link to a gist instead of descriptions 
of the code or changes you made. And example is worth 10000 words, and 
it makes it easier for people who are trying to help to understand 
exactly what you're dealing with.

Cheers,
David
Posted by Tony Spore (Guest)
on 2011-11-14 00:30
(Received via mailing list)
Sorry David,
I am getting this error:
ailure/Error: @record.save_cf_data(result)
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of Array.
       The error occurred while evaluating nil.first


Here is my Gist of my code that I can't figure out how to test. -
https://gist.github.com/1362863

So I am expecting it to send in my result, but instead the test has nil.


-Thanks,
Tony Spore
Posted by David Chelimsky (Guest)
on 2011-11-14 01:32
(Received via mailing list)
On Nov 13, 2011, at 4:54 PM, Tony Spore wrote:

>>
>> mock_model creates a test double, or pure mock ...
>>>
>>
>
>>
>>
>> Thanks again!
>
> I'd love to help, but ...
>
> 1. please post either inline or at the bottom (I moved your post to the bottom 
in this case - see http://idallen.com/topposting.html).
> 2. please post actual code or a link to a gist instead of descriptions of the 
code or changes you made. And example is worth 10000 words, and it makes it easier 
for people who are trying to help to understand exactly what you're dealing with.
>
> Cheers,
> David
>

Moving your post to the bottom again ....

> https://gist.github.com/1362863
>
> So I am expecting it to send in my result, but instead the test has nil.
The spec uses the string key "final_outputs", but the code uses the 
symbol key :final_outputs.

HTH,
David
Posted by Tony Spore (Guest)
on 2011-11-14 07:39
(Received via mailing list)
Thanks David.
I am getting a long way now.

-Thanks,
Tony Spore
CEO
SaasSoft LLC
(805) 253-1277
saassoft.com
Posted by Justin Ko (Guest)
on 2011-11-15 10:35
(Received via mailing list)
Tony, this text right here is considered "top posting" because it is 
*above* the previous posts. Now, scroll down and you'll see my "bottom 
post".

On Nov 13, 2011, at 11:03 PM, Tony Spore wrote:

>
>>>
>>>> describe Record do
>>>
>>>>       self.birth_day = result['birth_day'].first
>>>
>>>        You have a nil object when you didn't expect it!
>>>        The error occurred while evaluating nil.first
>> 2. please post actual code or a link to a gist instead of descriptions of the 
code or changes you made. And example is worth 10000 words, and it makes it easier 
for people who are trying to help to understand exactly what you're dealing with.
>>      NoMethodError:
> The spec uses the string key "final_outputs", but the code uses the symbol key 
:final_outputs.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

If you can see this, you're looking at a "bottom post". Do *this* for 
every reply on any mailing list. It makes it easier to follow 
conversations.
Sorry if it seems like I'm coming off as an ass, but David asked you to 
bottom post 3 times. Just want to make it clear ;)
Posted by Tony Spore (Guest)
on 2011-11-15 18:21
(Received via mailing list)
On Mon, Nov 14, 2011 at 11:49 PM, Justin Ko <jko170@gmail.com> wrote:

> Tony Spore
>> On Nov 13, 2011, at 4:54 PM, Tony Spore wrote:
>>> -Thanks,
>>>>     before  :each do
>>>>
>>>>      result = params[:final_outputs].first
>>>>
>>>>
>>>        You have a nil object when you didn't expect it!
>>>        You have a nil object when you didn't expect it!
>>>
>>> David
>>        The error occurred while evaluating nil.first
>> David
> rspec-users@rubyforge.org
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


Ok sorry didn't understand.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.