我在控制器中有以下方法:
def webhook
data_json = JSON.parse(request.body.read) # it comes from the test, it's OK
event = Stripe::Event.retrieve(data_json[:id]) # it's not OK, it's a real request to Stripe
stripe_cust_id = event.data.object.customer
user = User.where(stripe_customer_id: stripe_cust_id)
#.....
在spec文件中,我为event
创建了一个模拟,然后在测试中向webhook
发送了一个帖子请求。我不允许更改webhook
的正文或签名,因为我正在测试它。那么如何让它使用我创建的模拟?
describe '#webhook' do
it 'something' do
user = FactoryGirl.create(:user)
event = StripeMock.mock_webhook_event('invoice.payment_succeeded')
post(:webhook, event.to_json)
#error because webhook makes a real request to Stripe