How to mock/stub a model in Cucumber tests
Asked Answered
E

3

7

The scenario is as follows. My Order model has an after_create that contacts a remote payment gateway to retrieve a payment URL. In my Cucumber tests I don't want to perform this action, but return an arbitrary URL. My current cucumber tests looks like this:

Given there is a product "Product X" When I enter my credentials And I click "Order Now" Then I should be redirected to "arbitrary url"

The problem is where/how do I make sure that my order model sets the url correctly and does not contact the remote payment gateway?

Ellison answered 7/9, 2009 at 9:44 Comment(0)
E
4

In features/support/env.rb I monkey-patched my Order model to set the arbitrary URL. This could possible be done with Mocha or something else as well, but there is not point in this case.

In my steps I can check the response for the correct redirect like this:

Then /^I should be redirected to the payment gateway$/ do
  response.status.should eql("302 Found")
  response.location.should eql(Order.last.payment_url)
end

Hope this helps for others as well. I'd still like to know if there's a better/cleaner way of achieving this goal.

Ellison answered 7/9, 2009 at 10:28 Comment(0)
J
12

The wiki also has some tips on stubbing.

Johnsiejohnson answered 9/9, 2009 at 21:47 Comment(1)
Updated linkProthorax
E
4

In features/support/env.rb I monkey-patched my Order model to set the arbitrary URL. This could possible be done with Mocha or something else as well, but there is not point in this case.

In my steps I can check the response for the correct redirect like this:

Then /^I should be redirected to the payment gateway$/ do
  response.status.should eql("302 Found")
  response.location.should eql(Order.last.payment_url)
end

Hope this helps for others as well. I'd still like to know if there's a better/cleaner way of achieving this goal.

Ellison answered 7/9, 2009 at 10:28 Comment(0)
C
2

If I understand what you are trying to do correctly, have a look at FakeWeb.

Convincing answered 7/9, 2009 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.