How do I mock a Class with Ruby?

我的未来我决定 提交于 2019-12-21 15:09:45

问题


I'm using minitest/mock and would like to mock a class. I'm not trying to test the model class itself, but rather trying to test that a service (SomeService) interacts with the model (SomeModel).

I came up with this (Hack::ClassDelegate), but I'm not convinced it's a good idea:

require 'minitest/autorun'
require 'minitest/mock'

module Hack
  class ClassDelegate
    def self.set_delegate(delegate); @@delegate = delegate; end
    def self.method_missing(sym, *args, &block)
      @@delegate.method_missing(sym, *args, &block)
    end 
  end
end

class TestClassDelegation < MiniTest::Unit::TestCase

  class SomeModel < Hack::ClassDelegate ; end

  class SomeService
    def delete(id)
      SomeModel.delete(id)
    end
  end

  def test_delegation
    id = '123456789'
    mock = MiniTest::Mock.new
    mock.expect(:delete, nil, [id])

    SomeModel.set_delegate(mock)
    service = SomeService.new
    service.delete(id)

    assert mock.verify
  end
end

I'm pretty sure that mocking a class is not a great idea anyway, but I have a legacy system that I need to write some tests for and I don't want to change the system until I've wrapped some tests around it.


回答1:


I think that's a little complicated. What about this:

mock = MiniTest::Mock.new
SomeService.send(:const_set, :SomeModel, mock)
mock.expect(:delete, nil, [1])
service = SomeService.new
service.delete(1)
mock.verify
SomeService.send(:remove_const, :SomeModel)



回答2:


After running into the same problem and thinking about it for quite a while, I found that temporarily changing the class constant is probably the best way to do it (just as Elliot suggests in his answer).

However, I found a nicer way to do it: https://github.com/adammck/minitest-stub-const

Using this gem, you could write your test like this:

def test_delegation
  id = '123456789'
  mock = MiniTest::Mock.new
  mock.expect(:delete, nil, [id])

  SomeService.stub_const 'SomeModel', mock do
    service = SomeService.new
    service.delete(id)
  end

  assert mock.verify
end


来源:https://stackoverflow.com/questions/9585406/how-do-i-mock-a-class-with-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!