Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 若方法被发送到非模拟的对象,如何在MiniTest中断言?_Ruby On Rails_Ruby_Unit Testing_Mocking_Minitest - Fatal编程技术网

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