Ruby on rails 若方法被发送到非模拟的对象,如何在MiniTest中断言?
我知道我们可以使用Ruby on rails 若方法被发送到非模拟的对象,如何在MiniTest中断言?,ruby-on-rails,ruby,unit-testing,mocking,minitest,Ruby On Rails,Ruby,Unit Testing,Mocking,Minitest,我知道我们可以使用expect检查方法是否发送到模拟对象,但是检查方法是否发送到不是模拟的对象,而是应用程序中的实际对象呢 动机:Sandi Metz的“谈话”中说,对于单元测试传出的命令方法调用(在她的谈话中解释),我们应该验证消息是否已发送。我正在尝试这样做,但我在MiniTest中发现的唯一一件事是assert\u send,它有几个问题: 它已被弃用 它不考虑发送给接收对象的参数的值。如果我运行assert\u send([object,:method\u调用'argument 1','
expect
检查方法是否发送到模拟对象,但是检查方法是否发送到不是模拟的对象,而是应用程序中的实际对象呢
动机:Sandi Metz的“谈话”中说,对于单元测试传出的命令方法调用(在她的谈话中解释),我们应该验证消息是否已发送。我正在尝试这样做,但我在MiniTest中发现的唯一一件事是assert\u send
,它有几个问题:
assert\u send([object,:method\u调用'argument 1','argument 2'])
,则断言将返回true,即使object
所需的字符串与'argument 1'
和'argument 2'
不同我在网上搜索了两天的大部分时间。有人有什么想法吗?找到了我自己问题的答案:) 以防其他人像我一样遇到麻烦 使用gem可以断言已调用的方法 Ilija Eftimov的这篇文章-
class-BlogTest
这实际上非常简单,没有任何宝石!假设您有一个服务,NotificationService(注意这不是一个模拟),并且您希望断言对该服务调用了一个方法。您可以编写如下所示的测试:
def test_notification_is_sent_when_publishing
notification_service_spy = Spy.on(NotificationService, :notify_subscribers)
post = Post.new
user = User.new
blog = Blog.new(user)
blog.publish!(post)
NotificationService.expects(:notify_subscribers)
end
def test_notification_is_sent_when_publishing
notification_service_spy = Spy.on(NotificationService, :notify_subscribers)
post = Post.new
user = User.new
blog = Blog.new(user)
blog.publish!(post)
NotificationService.expects(:notify_subscribers)
end