用RSPEC通过类方法来调用ActActiverEcord方法
我很想做我想做的相当简单的事情。我有一个非常简单的类:
class User
def self.find_by_search(query)
query = query.to_s.strip
if query.upcase.start_with?("USER")
find(query)
else
find_by(name: "*#{query}*")
end
end
end
我想测试该逻辑,因此我只需要断言find
或find_by
与预期参数调用。我尝试了几件事:
User.find_by_search("USER0000")
expect(ActiveRecord::Base).to receive(:find)
两者
User.find_by_search("USER0000")
expect(User).to receive(:find) # didn't really think this would work, but figured I'd try anyway
都会在这里做一些愚蠢的
expected: 1 time with any arguments
received: 0 times with any arguments
事情,但似乎无法弄清楚。
I'm stumped on what appears to be a fairly simple thing I'd like to do. I've got a pretty simple class:
class User
def self.find_by_search(query)
query = query.to_s.strip
if query.upcase.start_with?("USER")
find(query)
else
find_by(name: "*#{query}*")
end
end
end
I'd love to test that logic, so I simply need to assert that either find
or find_by
is called with the expected parameter. I've tried a couple of things:
User.find_by_search("USER0000")
expect(ActiveRecord::Base).to receive(:find)
and
User.find_by_search("USER0000")
expect(User).to receive(:find) # didn't really think this would work, but figured I'd try anyway
Both result in
expected: 1 time with any arguments
received: 0 times with any arguments
I must be doing something dumb here, but can't seem to figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
期望()。在执行操作之前,应定义()
。阅读有关期望(...)。要接收(...) )
expect().to receive()
should be define before we execute the operation.Read more about expect(...).to receive(...)