【问题标题】:Rails Test Expects NILRails 测试期望 NIL
【发布时间】:2020-12-03 01:32:47
【问题描述】:

所以我正在编写一个单元测试来测试联系人是否被阻止......如果它被阻止,那么我希望联系人无法发送消息。 这是我正在尝试测试的后端逻辑

 if account
            ActsAsTenant.with_tenant(account) do
              contact = create_or_update_smooch_contact external_id, account.account_type, contact_name, nil, app_user[:_id]
              text = message[:text]
              if !contact.blocked 
                @message = Message.new(text: text, message_type: get_message_type(message[:type], message[:mediaType]),
                contact_id: contact.id, external_id: message[:_id], direction: "IN", account_id: account.id)
                
                if message[:quotedMessage]
                  # check if there is a quoted message
                  quoted_content =  message[:quotedMessage][:content]
                  reply_external_id = quoted_content[:_id]

                  quoted_message = Message.find_by(external_id: reply_external_id)
                  @message.reply_id = quoted_message.id if quoted_message
                end
                saved = @message.save!              
  end

这是单元测试代码

    test 'whatsapp blocked contact dont receive message' do
        account = accounts(:smooch_whatsapp)
        
        ActsAsTenant.with_tenant(account) do
            blocked_contact = contacts(:whatsapper)
            blocked_contact.blocked = true
            blocked_contact.save!
            
            before = Conversation.where(account: account).count
            payload = {"trigger": "message:appUser","app": {"_id": "blocked.smooch_app_user_id"},"version": "v1.1","messages": [{"type": "text","text": "Hi","role": "appUser","received": 15592934690.301,"name": "Trey","authorId": "b1f2003cd817cd4yyfaf8285","_id": "5cf0ee1c2c9d37000yy140c4","source": {"type": "whatsapp","integrationId": "smooch_whatsapp_integration_id", "originalMessageId": "ABEGJUcFhmVkAgo6FIL33ADyyeeex_y","originalMessageTimestamp": 1559293468}}],"appUser": {"_id": "b1f2003cd817cd4d6faf8285", "userId": '254712345678',"conversationStarted": true,"givenName": "Trey","signedUpAt": "2019-05-31T09:04:28.266Z","properties": {}},
"conversation": {"_id": "18884d5dbdd138efaa088cc3"},"client": {"integrationId": "5b7810f77c2a2b0022b1f69c","externalId": "blocked.external_id","id": "d776004d-c8d2-4aaa-ba24-96dd65b6ddfcc",
"displayName": "+254 712 345 678","status": "active", "raw": {
"from": '254712345678', "profile": {"name": "Trey"}}}}

            payload[:format] = "json"
            post smooch_messages_path, params: payload
            assert_response :success

            after = Conversation.where(account: account).count

            message = Message.find_by(external_id: "b1f2003cd817cd4d6faf8285")
            assert_nil message
            assert_equal before, after
        end
    end

测试一直失败,因为如果联系人被阻止,它希望消息为 nil

预期 # 为零。

我该如何解决??

【问题讨论】:

  • 如果没有后端逻辑,很难回答你的测试失败的原因。
  • @AlterLagos 我添加了后端逻辑

标签: ruby-on-rails ruby unit-testing testing smooch


【解决方案1】:

将消息添加到对话记录和将这些消息传送到第 3 方渠道是单独的操作。 前者的失败将在请求上引发 HTTP 错误。 后者的失败只会通过message:delivery:failure webhooks 事件进行通信:https://docs.smooch.io/guide/delivery-events/#delivery-failure

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2012-06-06
    • 1970-01-01
    相关资源
    最近更新 更多