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

前端 未结 6 943
春和景丽
春和景丽 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:27

    What I like to do is create a new host class and mix the module into it, something like this:

    describe MyModule do
      let(:host_class) { Class.new { include MyModule } }
      let(:instance) { host_class.new }
    
      describe '#instance_method' do
        it 'does something' do
          expect(instance.instance_method).to do_something
        end
      end
    end
    

提交回复
热议问题