Ruby on rails rails中的单元测试-带回形针的模型

Ruby on rails rails中的单元测试-带回形针的模型,ruby-on-rails,ruby,unit-testing,paperclip,Ruby On Rails,Ruby,Unit Testing,Paperclip,我正试着用回形针为一个有图片的模型写一个测试。我使用的是测试框架默认值,不应该使用shoulda或rspec。在这种情况下,我应该如何测试它?我真的应该上传一个文件吗?如何将文件添加到夹具?将文件添加到模型非常简单。例如: @post = Post.new @post.attachment = File.new("test/fixtures/sample_file.png") # Replace attachment= with the name of your paperclip attach

我正试着用回形针为一个有图片的模型写一个测试。我使用的是测试框架默认值,不应该使用shoulda或rspec。在这种情况下,我应该如何测试它?我真的应该上传一个文件吗?如何将文件添加到夹具?

将文件添加到模型非常简单。例如:

@post = Post.new
@post.attachment = File.new("test/fixtures/sample_file.png")
# Replace attachment= with the name of your paperclip attachment
在这种情况下,您应该将文件放入
test/fixtures
dir

我通常在test_helper.rb中做一个小助手

def sample_file(filename = "sample_file.png")
  File.new("test/fixtures/#{filename}")
end
然后


如果您使用类似的设备来代替固定装置,这将变得更加容易。

这在Rspec中,但可以轻松切换

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
由于回形针经过了很好的测试,我通常不会太关注“上传”的行为,除非我在做一些不同寻常的事情。但是,我会尽量集中精力确保附件的配置,相对于它所属的模型,满足我的需要

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

所有的好东西都可以在
Model.attachment\u definitions
中找到

我使用FactoryGirl,设置模型

#photos.rb
FactoryGirl.define do
  factory :photo do
    image File.new(File.join(Rails.root, 'spec', 'fixtures', 'files', 'testimg1.jpg'))
  description "testimg1 description"
  end # factory :photo
 end
然后

在回形针附件中指定其保存位置,即

...
the_path= "/:user_id/:basename.:extension"
if Rails.env.test?
   the_path= ":rails_root/tmp/" + the_path
end
has_attached_file :image,  :default_url => ActionController::Base.helpers.asset_path('missing.png'),
:path => the_path, :url => ':s3_domain_url'

Paperclip.interpolates :user_id do |attachment, style|
   attachment.instance.user_id.to_s
end

然后测试附件_定义(由kwon建议)和Dir.glob以检查文件是否保存

 it "saves in path of user.id/filename" do
    expect(Dir.glob(File.join(Rails.root, 'tmp', @user.id.to_s, @user.photo.image.instance.image_file_name)).empty?).to be(false)
 end

通过这种方式,我确信它执行正确的直接/路径创建等

它给出:NoMethodError:undefined方法'attachment='@CanCeylan
attachment
是通用的,就像
foo
;在模型上添加回形针附件时,您应该将其替换为您使用的任何名称。将其添加到post Fixture中如何?什么是@valid_属性?在我的测试用例中,这只是创建模型所需的属性散列。您自己的测试可能不需要这些。
...
the_path= "/:user_id/:basename.:extension"
if Rails.env.test?
   the_path= ":rails_root/tmp/" + the_path
end
has_attached_file :image,  :default_url => ActionController::Base.helpers.asset_path('missing.png'),
:path => the_path, :url => ':s3_domain_url'

Paperclip.interpolates :user_id do |attachment, style|
   attachment.instance.user_id.to_s
end
 it "saves in path of user.id/filename" do
    expect(Dir.glob(File.join(Rails.root, 'tmp', @user.id.to_s, @user.photo.image.instance.image_file_name)).empty?).to be(false)
 end