我有以下代码:
Group.where('name ~* ?', params[:name]).first
在这种情况下如何对 where
方法进行 stub ?
Group.stub(:where).and_return(mock_model(Group, name: "SomeName"))
导致错误:
Mock "Group_1001" received unexpected message :first with (no args)
最佳答案
在您的情况下,您应该首先返回响应 的内容。数组是一个不错的选择。
Group.stub(:where).and_return([mock_model(Group, name: "SomeName")])
关于ruby-on-rails - 如何 stub :where method in Rspec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15383474/