cfcosta / minitest-firemock

Makes your MiniTest mocks more resilient.
31 stars 1 forks source link

Class/Constant Mocks #5

Open plukevdh opened 9 years ago

plukevdh commented 9 years ago

I want to have this (and am totally willing to do the legwork to make a PR for it), but I'm curious about how you want to go about it, as Minitest proper doesn't really seem to provide mocking of Class/Constants. That falls more into the domain of stubbing.

If you could provide some insight into the interface you expect this to have, I'm happy to put forward some code to support.

Thanks

cfcosta commented 9 years ago

I was thinking here... What if we do something like this:

class MyClass
  def self.my_method
    20
  end
end

it "works on existing class methods" do
  my_mock = MiniTest::FireMock.new('MyClass')
  mymock.class.expect(:my_method,42)
  assert_equal 42, mock.class.my_method
  mock.verify
end

it "fails on non-existing class methods" do
  my_mock = MiniTest::FireMock.new('MyClass')
  mymock.class.expect(:my_omg,42)
  assert_equal 42, mock.class.my_method
  mock.verify #should fail
end

That could be accomplished generating anonymous classes when you instantiate MiniTest::Firemock, but it might be too much magic. Another, simpler idea is to just expect_class_method(:method).