我正在尝试制作一个stub_request
,以将所有请求与 fcm.googleapis.com 匹配。我们的后端应该在创建新的帖子或评论时向其用户发送推送通知。我们的测试触发了对fcm.googleapis.com的大量请求,这就是为什么我需要通用匹配器。
编辑:测试失败,因为我已将stub_request
添加到spec_helper中。失败的测试不是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 => {})
在创建新帖子时,我的后端应该向我们的用户发送推送通知。
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 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
答案 0 :(得分:0)
测试失败,因为我已将stub_request添加到spec_helper中。失败的测试不是rspec,而是普通的ActionController :: TestCase测试。我的错! :-|