On Sunday, February 9, 2014 6:40:13 AM UTC-8, siva wrote:
>
>
>   
>  Thanks Xavier it worked. But I need a small clarification. How could we 
> stub chain of methods where each method asks a set of arguments?
>
> On Friday, 7 February 2014 08:55:28 UTC+5:30, Xavier Shay wrote:
>>
>>  
>> On 5/02/14 10:40 PM, siva wrote:
>>  
>>
>>   Hi Xavier 
>>
>>    Thanks for your reply.
>>
>>    I want assert whether `calculate` has been called no. of times that 
>> loop has been iterated with the same parameters every time.
>>
>>   And on class `InterestAmount` initializer I set the parameters in 
>> instance variables like as  follows so that I can use same instance 
>> variables across all methods in `InterestAmount` class.
>>
>>   class InterestAmount
>>    def initialize(installment, date, project)
>>     @installment = installment
>>     @project = project
>>     @date = date
>>   end
>>   end
>>  
>> If you do:
>>
>> class InterestAmount
>>   def self.calculate(*args); new(*args).calculate; end
>>   #...
>> end
>>
>> you hide the initializer, and subsequently your tests get much nicer. 
>> (i.e. listening to the test leads to a nicer interface)
>>
>>  
>>
>>
>>
>> On Wednesday, 5 February 2014 10:42:44 UTC+5:30, Xavier Shay wrote: 
>>>
>>>
>>> On 4/02/14 1:05 AM, siva wrote: 
>>> > 
>>> > 
>>> >    Hi all 
>>> > 
>>> >    In my controller I have code as below: 
>>> > 
>>> >       # app/controllers/bookings_controller.rb 
>>> > 
>>> >      @booking.unit_installments.each do |i| 
>>> >         InterestAmount.new(i, Date.today, project).calculate 
>>> >       end 
>>> > 
>>> >      I have seen `stub_chain` method but I didn't find how to stub 
>>> > chain methods with arguments and how to verify chain methods with 
>>> > arguments as well. Could you help me? 
>>> What are you trying to test/assert here? 
>>> At a first glance, I'd refactor InterestAmount to provide an interface 
>>> `InterestAmount.calculate`, then you don't need to use stub_chain, and 
>>> also hide your implementation. 
>>>
>>> Cheers, 
>>> Xavier 
>>> > 
>>> >     Thanks in advance. 
>>> > --
>>
>>
You can stub any arbitrary chain of methods by setting up the stubs 
individually:

allow(a).to receive(:foo).with(1).and_return(b = double)
allow(b).to receive(:bar).with(2).and_return(c = double)
allow(c).to receive(:baz).with(3).and_return("it worked")

expect(a.foo(1).bar(2).baz(3)).to eq("it worked")

HTH,
Myron

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/5f717454-cc79-45ac-a1ad-ad7bea252e09%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to