gpt4 book ai didi

ruby-on-rails - 为什么我在 rspec 中得到事件记录关系而不是数组?

转载 作者:行者123 更新时间:2023-12-03 17:34:55 25 4
gpt4 key购买 nike

我正在 Post 上运行测试:

expect(subject.query).to eq [previous_post, post, future_comment]

但我收到此错误:

expected: [#<Post id: 3, body: "Sed quis aut harum. Asperiores ad commodi tempore ...", ...>]


got: #<ActiveRecord::Relation [#<Post id: 3, body: "Sed quis aut harum. Asperiores a.....>]>

它需要一个数组,但得到一个事件记录对象。我该如何解决?

这是我正在测试的代码:

      def query
scope = Post
.where(user_id: user.id)
.my_scope
.my_other_scope
.includes(:body)

scope = case @options[:order_by]
when "older"
order_by_older(scope)
when "most_discussed"
order_by_most_discussed(scope)
else
order_by_older(scope)
end

scope
end

最佳答案

您可以更新要使用的规范。

expect(subject.query).to contain_exactly(previous_post, post, future_comment)

如果它在您的代码中是一个依赖项,那么您可以使用将其转换为数组@MrYoshiji 提到的 .to_a

如果可以避免,我不建议将其转换为数组,因为它会更慢。

关于ruby-on-rails - 为什么我在 rspec 中得到事件记录关系而不是数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47538214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com