Should I stub the model in Factory girl or in the spec file while testing?

前端 未结 4 2174
深忆病人
深忆病人 2021-02-18 12:39

Almost every spec file I come accross I end up writing stuff like:

  before :each do
    @cimg = Factory.build :cimg_valid
    @cimg.stub(:validate_img).and_retu         


        
4条回答
  •  不要未来只要你来
    2021-02-18 13:40

    In recent versions of factory_girl you have an after_build callback, so I believe you could define your factory like this:

    FactoryGirl.define do
      factory :cimg_for_testing_tags do
    
        ... # Factory attributes
    
        after_build do |cimg|
          cimg.stub(:validate_img).and_return true
        end
      end
    end
    

    UPDATE

    After factory_girl 3.3.0, the syntax has changed to following:

    FactoryGirl.define do
      factory :cimg_for_testing_tags do
    
        ... # Factory attributes
    
        after(:build) do |cimg|
          cimg.stub(:validate_img).and_return true
        end
      end
    end
    

提交回复
热议问题