beatrichartz / exchange

Easy Currency Operations directly available on your numbers - maintenance discontinued
http://beatrichartz.github.com/exchange
MIT License
42 stars 22 forks source link

Stub to/in method in with rspec #9

Closed fernandomm closed 9 years ago

fernandomm commented 9 years ago

Is there a way to stub the to/in methods when using rspec?

I tried something like:

Exchange::Money.any_instance.stub(:to).and_return(Exchange::Money.new(1.20, :eur))
Exchange::Money.any_instance.stub(:in).and_return(Exchange::Money.new(1.20, :eur))

But it keeps using the original methods.

Example: when i pass an invalid currency I still get an Exchange::NoCurrencyError exception.

beatrichartz commented 9 years ago

Depends on which of the in you want to stub:

If you want to stub the in in 1.in(:eur), the way to go is to stub new on Exchange::Money:

my_money = Exchange::Money.new(1.20, :eur) #put this in a let!: let!(:my_money) { ... }
allow(Exchange::Money).to receive(:new).and_return my_money

if you want to stub a conversion (e.g. 1.in(:eur).to(:usd)) you could stub any instance like you did before. If that doesn't work, I'd have to know a little bit more about your test setup.