ruby-on-rails - 服务调用后如何检查 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
解决方案
可能为时已晚,但如果有人遇到同样的问题,它可能会很有用。
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
推荐阅读
- javascript - 如何从容器外部创建并连接到 docker 容器中的 TTY?
- video - ffmpeg concat 去除视频声音
- swift - 鼠标事件不会路由到 Cocoa 应用程序中的 NSButtons
- python - 是否可以同时开始多个培训?
- jmeter - JMeter:录制时解压缩请求
- java - 在 Java 中获取自定义对象大小
- python - 我们可以在 nuitka 或 cython 中使用 pyqt 线程吗?
- javascript - 如何使用护照在登录中修复此错误
- javascript - 如何创建一个先快后慢的区间?
- kubernetes - Kubernetes,活性探测失败,但 pod 处于运行状态