unit test in rails - model with paperclip

前端 未结 3 971
忘掉有多难
忘掉有多难 2021-01-31 16:27

I\'m trying to write a test for a model with a picture, using paperclip. I\'m using the test framework default, no shoulda or rspec. In this context, how should I test it? Shoul

3条回答
  •  Happy的楠姐
    2021-01-31 16:43

    This is in Rspec, but can be easily switched over

    before do # setup
      @file = File.new(File.join(RAILS_ROOT, "/spec/fixtures/paperclip", "photo.jpg"), 'rb')
      @model = Model.create!(@valid_attributes.merge(:photo => @file))
    end
    
    it "should receive photo_file_name from :photo" do # def .... || should ....
      @model.photo_file_name.should == "photo.jpg"
      # assert_equal "photo.jpg", @model.photo_file_name
    end
    

    Since Paperclip is pretty well tested, I usually don't focus too much on the act of "uploading", unless i'm doing something out of the ordinary. But I will try to focus more on ensuring that the configurations of the attachment, in relation to the model it belongs, meet my needs.

    it "should have an attachment :path of :rails_root/path/:basename.:extension" do
      Model.attachment_definitions[:photo][:path].should == ":rails_root/path/:basename.:extension"
      # assert_equal ":rails_root/path/:basename.:extension", Model.attachment_definitions[:photo][:path]
    end
    

    All the goodies can be found in Model.attachment_definitions.

提交回复
热议问题