2016-11-17 115 views
1

我刚刚注意到,设置了默认值后,在请求前无法更改值。RSpec请求测试 - 在模拟发送请求之前分配更改值

当我检查处理此请求的方法内的值时,它显示event.data.object.closed = false

以下代码仅仅是一个例子。

require 'rails_helper' 

describe "stripe_invoice_created_webhook", type: :request do 

    let(:invoice){ create(:invoice, account_id: account.id) } 
    let(:account){ create(:account, 
         stripe_customer_id: event.data.object.customer)} 
    let(:event){ StripeMock.mock_webhook_event('invoice.created', { 
    closed: false 
    }) } 

    it 'responds 200 to invoice_created webhook with valid endpoint' do 
    event.data.object.closed = true 
    post '/stripe-events', event.as_json 
    expect(response.status).to eq 200 
    end  
end 

任何想法如何正确发送一些

===更新(2016年11月17日)===========

的主要问题是前更改值如何有效更改模拟值?

我可以写每个类似的东西。

event = StripeMock.mock_webhook_event('invoice.created', {closed: true }) 

它的工作原理,主要问题是,我怎样才能使它更清洁与let(:foo)像往常一样?

+0

你能分享你想要编写代码测试? –

回答

0

您的mock_webhook_event只是一个模拟,对正确调用的伪响应。你要做的就是改变模拟响应,就好像它是实际的对象一样。

更好(虽然稍微详细)将使用变量值

let(:closed_response) {false} 

    let(:event){ StripeMock.mock_webhook_event('invoice.created', { 
    closed: closed_response 
    }) } 

然后在您的测试...

closed_response = true 
post '/stripe-events', event.as_json 
+0

感谢您的答案!但我仍然得到相同的结果...'虚假'的价值。 – Tosh

+0

你是否在'closed_response'赋值之前引用'event'?一旦你引用它,':closed'的值被锁定。你应该设置'closed_response = true'作为你的测试的第一行。 – SteveTurczyn

+0

'closed_response'分配位于测试块的行的顶部。 – Tosh