[rspec-users] Testing Custom Methods

Tony Spore tspore at saassoft.com
Sat Nov 12 19:27:11 EST 2011


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


On Sat, Nov 12, 2011 at 2:14 PM, David Chelimsky <dchelimsky at gmail.com>wrote:

> 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
> 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
>
>
> ... 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/mocks/mock-model and
> https://www.relishapp.com/rspec/rspec-rails/docs/mocks/stub-model for
> more info.
>
> HTH,
> David
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20111112/d4101a25/attachment.html>


More information about the rspec-users mailing list