无法存根外部请求 - 错误的正则表达式?
Unable to stub external request - Wrong regexp?
我正在尝试制作一个 stub_request
来匹配对 fcm.googleapis.com 的所有请求。我们的后端应该在创建新的 Posts 或 Comments 时向其用户发送推送通知。我们的测试触发了很多对 fcm.googleapis.com 的请求,这就是为什么我需要一个通用匹配器。
编辑: 测试失败,因为我在 spec_helper 中添加了 stub_request
。失败的测试不是 rspec,而是普通的 ActionController::TestCase
测试。我的错! :-|
spec/spec_helper.rb
18 RSpec.configure do |config|
19 require_relative "../test/mock_helper"
20 require "webmock/rspec"
21
22 WebMock.disable_net_connect!
23 config.before(:each) do
24 stub_request(:post, "https://fcm.googleapis.com/fcm/send").
25 with(body: /.*/).
26 to_return(status: 200)
27
但是当我 运行 测试时,WebMock 看起来并不关心我的 stub_request。可能出了什么问题?
运行 测试
Error:
PostsControllerTest#test_should_create_post:
WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled.
Unregistered request: POST https://fcm.googleapis.com/fcm/send with body
'{"registration_ids":[null],"notification":
{"title":"Fred Flintstone har skrevet en melding i Bedrock Sportsballteam",
"text":"New post!?"},"data":{"notification":{"avatar":null,"group_id":131900578,
"issued_at":"2018-06-25T13:37:28.746+02:00",
"full_name":"Fred Flintstone","id":700,"post_id":980190963,
"role_id":1}}}' with headers {'Authorization'=>'key=KEY', 'Content-Type'=>'application/json'}
You can stub this request with the following snippet:
stub_request(:post, "https://fcm.googleapis.com/fcm/send").
with(:body => "{\"registration_ids\":[null],\"notification\":
{\"title\":\"Fred Flintstone har skrevet en melding i Bedrock Sportsballteam\",
\"text\":\"New post!?\"},\"data\":{\"notification\":
{\"avatar\":null,\"group_id\":131900578,
\"issued_at\":\"2018-06-25T13:37:28.746+02:00\",
\"full_name\":\"Fred Flintstone\",\"id\":700,\"post_id\":980190963,\"role_id\":1}}}",
:headers => {'Authorization'=>'key=KEY',
'Content-Type'=>'application/json'}).
to_return(:status => 200, :body => "", :headers => {})
当一个新的 Post 创建更新时,我的后端应该向我们的用户发送推送通知。
app/models/post.rb
16 class Post < ApplicationRecord
25 after_save :send_notifications
82 def send_notifications
83 PUSH_NOTIFICATIONS.new_post_in_group(post: self)
84 end
bin/rails 测试 test/controllers/posts_controller_test.rb:57
57 test "should create post" do
58 assert_difference("Post.count") do
59 assert_difference("PostImage.count", 3) do
60 post :create, params: {
61 group_id: groups(:sportsball).id,
62 post: {
63 text: "New post!?",
64 is_pinned: "true"
73 }
74 }
75
76 post = Post.last
77 assert_equal true, post.is_pinned
78
79 assert_response :created, response.body
80 assert valid_json?(response.body), "Invalid json: #{response.body}"
81
82 json = JSON.parse(response.body).deep_symbolize_keys
83
84 end
85 end
86 end
推送通知
class PushNotifications
def initialize
@fcm = FCM.new(ENV["FCM_SERVER_KEY"])
end
def new_post_in_group(post:)
registration_ids = all_users_except_author(post)
author = post.user
group = post.group
return unless registration_ids
options = {
notification: {
title: "#{author.name} har skrevet en melding i #{group.name}",
text: post.text.truncate(27)
},
data: {
notification:
{
avatar: author.avatar,
# comment_id: '646',
group_id: group.id,
issued_at: Time.now,
full_name: author.name,
id: 700, # 700 = new post. The client knows what to do by looking at this id.
post_id: post.id,
role_id: author.role_id(group)
}
}
}
response = @fcm.send(registration_ids, options)
puts "Sendt: #{response}" if ENV["DEBUG"]
end
private
def all_users_except_author(post)
recipients = post.group.users.pluck(:fcm_token)
recipients.delete(post.user.id)
recipients
end
end
config/initializers/PushNotifications.rb
1 require "#{Rails.root}/lib/push_notifications"
2
3 puts "initialize PushNotifications"
4 PUSH_NOTIFICATIONS ||= PushNotifications.new
测试失败,因为我将 stub_request 添加到 spec_helper。失败的测试不是 rspec,而是普通的 ActionController::TestCase 测试。我的错! :-|
我正在尝试制作一个 stub_request
来匹配对 fcm.googleapis.com 的所有请求。我们的后端应该在创建新的 Posts 或 Comments 时向其用户发送推送通知。我们的测试触发了很多对 fcm.googleapis.com 的请求,这就是为什么我需要一个通用匹配器。
编辑: 测试失败,因为我在 spec_helper 中添加了 stub_request
。失败的测试不是 rspec,而是普通的 ActionController::TestCase
测试。我的错! :-|
spec/spec_helper.rb
18 RSpec.configure do |config|
19 require_relative "../test/mock_helper"
20 require "webmock/rspec"
21
22 WebMock.disable_net_connect!
23 config.before(:each) do
24 stub_request(:post, "https://fcm.googleapis.com/fcm/send").
25 with(body: /.*/).
26 to_return(status: 200)
27
但是当我 运行 测试时,WebMock 看起来并不关心我的 stub_request。可能出了什么问题?
运行 测试
Error:
PostsControllerTest#test_should_create_post:
WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled.
Unregistered request: POST https://fcm.googleapis.com/fcm/send with body
'{"registration_ids":[null],"notification":
{"title":"Fred Flintstone har skrevet en melding i Bedrock Sportsballteam",
"text":"New post!?"},"data":{"notification":{"avatar":null,"group_id":131900578,
"issued_at":"2018-06-25T13:37:28.746+02:00",
"full_name":"Fred Flintstone","id":700,"post_id":980190963,
"role_id":1}}}' with headers {'Authorization'=>'key=KEY', 'Content-Type'=>'application/json'}
You can stub this request with the following snippet:
stub_request(:post, "https://fcm.googleapis.com/fcm/send").
with(:body => "{\"registration_ids\":[null],\"notification\":
{\"title\":\"Fred Flintstone har skrevet en melding i Bedrock Sportsballteam\",
\"text\":\"New post!?\"},\"data\":{\"notification\":
{\"avatar\":null,\"group_id\":131900578,
\"issued_at\":\"2018-06-25T13:37:28.746+02:00\",
\"full_name\":\"Fred Flintstone\",\"id\":700,\"post_id\":980190963,\"role_id\":1}}}",
:headers => {'Authorization'=>'key=KEY',
'Content-Type'=>'application/json'}).
to_return(:status => 200, :body => "", :headers => {})
当一个新的 Post 创建更新时,我的后端应该向我们的用户发送推送通知。
app/models/post.rb
16 class Post < ApplicationRecord
25 after_save :send_notifications
82 def send_notifications
83 PUSH_NOTIFICATIONS.new_post_in_group(post: self)
84 end
bin/rails 测试 test/controllers/posts_controller_test.rb:57
57 test "should create post" do
58 assert_difference("Post.count") do
59 assert_difference("PostImage.count", 3) do
60 post :create, params: {
61 group_id: groups(:sportsball).id,
62 post: {
63 text: "New post!?",
64 is_pinned: "true"
73 }
74 }
75
76 post = Post.last
77 assert_equal true, post.is_pinned
78
79 assert_response :created, response.body
80 assert valid_json?(response.body), "Invalid json: #{response.body}"
81
82 json = JSON.parse(response.body).deep_symbolize_keys
83
84 end
85 end
86 end
推送通知
class PushNotifications
def initialize
@fcm = FCM.new(ENV["FCM_SERVER_KEY"])
end
def new_post_in_group(post:)
registration_ids = all_users_except_author(post)
author = post.user
group = post.group
return unless registration_ids
options = {
notification: {
title: "#{author.name} har skrevet en melding i #{group.name}",
text: post.text.truncate(27)
},
data: {
notification:
{
avatar: author.avatar,
# comment_id: '646',
group_id: group.id,
issued_at: Time.now,
full_name: author.name,
id: 700, # 700 = new post. The client knows what to do by looking at this id.
post_id: post.id,
role_id: author.role_id(group)
}
}
}
response = @fcm.send(registration_ids, options)
puts "Sendt: #{response}" if ENV["DEBUG"]
end
private
def all_users_except_author(post)
recipients = post.group.users.pluck(:fcm_token)
recipients.delete(post.user.id)
recipients
end
end
config/initializers/PushNotifications.rb
1 require "#{Rails.root}/lib/push_notifications"
2
3 puts "initialize PushNotifications"
4 PUSH_NOTIFICATIONS ||= PushNotifications.new
测试失败,因为我将 stub_request 添加到 spec_helper。失败的测试不是 rspec,而是普通的 ActionController::TestCase 测试。我的错! :-|