1

我对一些发送 SMS 消息的代码进行了集成测试。

测试发送初始消息,例如“Hello, world”,并使用 VCR 验证对外部 SMS 提供商的调用是通过 HTTP API 进行的。

然后测试模拟通过控制器发送对 SMS 消息的回复。

收到无法辨认的回复后,代码将发送一条消息,例如“抱歉。不计算”。然后重新发送“Hello, world”。

测试寻找发送“Sorry...”的调用,然后寻找重新发送“Hello, world”的调用。

但是,似乎代码没有重新发送第二个“Hello, world”,但测试认为它是,因为第二个expect a_request没有失败。

我正在使用自定义match_requests_on: [:uri, :method]来匹配methodand uri。由于第一个“Hello, world”和第二个基本相同,这条规则是否会产生我的误报?我关闭了重复,所以我希望两个单独的请求必须在磁带中,否则测试会失败。

我错过了什么?

4

1 回答 1

0

There are several things you can look at.

  1. I think it is a problem that the VCR exception is not being raised. Are you using any async libraries (e.g. Celluloid, ruby threads, delayed_job) for concurrency or background jobs? Something must be catching and handling the exception by printing it, which could explain why you see it, but it is not reported to rspec.

  2. Your vcr.log does contain 2 interactions with twilio's Message.json, which means that with :allow_playback_repeats => false (the default), VCR will respond to this method/URI twice.

    [post https://SomeUUID:SomeSecretKey@api.twilio.com/2010-04-01/Accounts/SomeUUID/Messages.json] => [201 "{\"sid\": \"SMea17b16d31d34ce9b9ad8c6e68987b31\", \"date_created\": \"Tue, 22 Dec 2015 "], 
    [post https://SomeUUID:SomeSecretKey@api.twilio.com/2010-04-01/Accounts/SomeUUID/Messages.json] => [201 "{\"sid\": \"SM29ac83b5e668432e901dd75cba7bc674\", \"date_created\": \"Tue, 22 Dec 2015 "]
    

    Note: With :record => :once, VCR will record as many interactions/requests as you make, and then will freeze the cassette as read-only afterward.

  3. Search your vcr.log for [Cassette: 'cms/sms_08'] Initialized with options. You'll notice that the cassette is being loaded twice (i.e. for two tests). Then search for for [post https://SomeUUID:SomeSecretKey. You'll notice that in the first, there are two Message.json requests, both of which are served successfully. In the second test, a 3rd request is made, which webmock identifies as unhandled, causing the exception you see.

  4. You said you're using expect a_request. Are you using it with .twice to specify how many times? https://github.com/bblimke/webmock#setting-expectations-in-rspec-on-webmock-module

于 2015-12-23T11:13:12.687 回答