Testing a concern / module that uses ActiveRecord

后端 未结 4 548
你的背包
你的背包 2020-12-30 04:25

SCENARIO I have extracted a concern called Taggable. It\'s a module that allows any model to support tagging. I have included

4条回答
  •  太阳男子
    2020-12-30 05:06

    Here is my solution of similar problem:

    describe Taggable do
      subject { mock_model('User').send(:extend, Taggable) }
    
      it { should have_many(:tags) }
      ...
    
      describe "#tag" do
        ...
      end
    end
    

    In fact mock_model('User') can mock any existent model in the system.

    This is not an ideal solution but at least it's clear and mocks everything.

    Note: mock_model (AR mocks) were extracted to rspec-activemodel-mocks in rspec 3.0.
    Also you need to use shoulda-matchers for associations matchers.

提交回复
热议问题