首页 > 解决方案 > 服务调用后如何检查 rspec 中的数组更改?

问题描述

目标很简单

例如我们有一个数组

[
  {name: "ghost", state: "rejected"}, 
  {name: "donkey", state: "rejected"}
]

运行 Service 调用后UpdateAllUsers,它将所有用户更改为'accepted'

   [
      {name: "ghost", state: "accepted"}, 
      {name: "donkey", state: "accepted"}
    ]

这里的问题是如何在 rspec 中的该服务调用之后检查数组中的所有对象是否具有相同的值?

rspec 中的示例

describe 'call' do
   let(:all_users) { build(:users, 2, state: "rejected")

    service_call { UpdateAllUsers.new.call }


    it "change all user state to approved" do
         # The goal is to check all the users state after the Service call.
         # Can't find a way to do so
         # the after state should be state => 'accepted'
     end
end

标签: ruby-on-railsrspecrspec-rails

解决方案


可能为时已晚,但如果有人遇到同样的问题,它可能会很有用。

describe 'call' do
   let(:all_users) { build(:users, 2, state: "rejected")

    service_call { UpdateAllUsers.new.call }


    it "change all user state to approved" do
         # The goal is to check all the users state after the Service call.
         # Can't find a way to do so
         # the after state should be state => 'accepted'
        all_users.each do |user|
          expect(user).to include(:state => 'accepted')
        end
    end
end

推荐阅读