Rails: How do I write tests for a ruby module?

前端 未结 6 898
春和景丽
春和景丽 2021-01-31 02:22

I would like to know how to write unit tests for a module that is mixed into a couple of classes but don\'t quite know how to go about it:

  1. Do I test the instanc

6条回答
  •  故里飘歌
    2021-01-31 02:43

    Use inline classes (I am not doing any fancy flexmock or stubba/mocha usage just to show the point)

    def test_should_callout_to_foo
       m = Class.new do
         include ModuleUnderTest
         def foo
            3
         end
       end.new
       assert_equal 6, m.foo_multiplied_by_two
     end
    

    Any mocking/stubbing library out there should give you a cleaner way to do this. Also you can use structs:

     instance = Struct.new(:foo).new
     class<

    If I have a module that is being used in many places I have a unit test for it which does just that (slide a test object under the module methods and test if the module methods function properly on that object).

提交回复
热议问题