On Oct 28, 2011, at 10:16 AM, internetchris wrote:
> Hi Group
>
> I''m still waiting for the light to come on with stubbing/mocking
and
> the syntax for each. I have simple method in my Order class like
> so....
>
> def credit_card
> ActiveMerchant::Billing::CreditCard.new(
> :type => card_type,
> :number => card_number,
> :verification_value => card_verification,
> :month => card_expires_on.blank? ? Time.now.month :
> card_expires_on.month,
> :year => card_expires_on.blank? ? Time.now.year :
> card_expires_on.year,
> :first_name => first_name,
> :last_name => last_name
> )
> end
>
> This method should return valid if the card is valid.
>
http://activemerchant.rubyforge.org/classes/ActiveMerchant/Billing/CreditCard.html
>
> I started out like this but then confusion set in. If anyone has the
> time to help me understand or write the proper spec for this method I
> think it will help me better understand how to stub or mock a 3rd
> party item like this. I just watched the peepcode screencast and
I''ve
> watched the railscasts. I''m just trying to apply my meager
> understanding to my own project now.
>
> it "should validate credit card" do
> gateway = stub(''gateway'')
> gateway.should_recieve(:valid)
> ActiveMerchant::Billing::CreditCard.stub(:new).and_return
> gateway
> end
This RSpec example has nothing to do with a gateway. Also, it is a credit card
object, which does not talk to any third party service. You don''t need
to use any mocking here.
You probably want to make sure the credit_card *can* be valid. So to do so,
you''ll need to create a valid user (or whatever model this credit_card
method is in:
describe User do
describe ''#credit_card'' do
context ''with a valid user'' do
let(:user) { create(:user) }
it ''returns a valid credit card instance'' do
user.credit_card.should
be_an_instance_of(ActiveMerchant::Billing::CreditCard)
user.credit_card.should be_valid
end
end
end
end
Now, your User factory would fill in all the credit card fields so the credit
card is able to be valid. I would also check that the credit card has the
correct attributes:
describe User do
describe ''#credit_card'' do
context ''with a valid user'' do
let(:user) { create(:user, card_type: ''Visa'') }
describe ''the returned credit card instance'' do
it ''has the correct card_type'' do
user.credit_card.card_type.should eq(''Visa'')
end
end
end
end
end
As far as mocking third party gateway services, ActiveMerchant provides built in
support to switch a gateway to its "test" mode, which you would do in
your test.rb environment. ActiveMerchant has good documentation for all of this.
Hope that helps.
>
> I also have a credit card Factory setup, but I''m not sure where or
how
> I apply this to the stub.
>
> Thanks in advance!
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users