我完全不懂 Rspec。今晚写我的第一个测试。
我有一个名为 Image 的模型。使用 paperclip 我附加了一个名为 photo 的文件。标准的事情。我运行了 paperclip 生成器,在生产和测试模式下一切都正常。
现在我有一个名为 image.rb 的 spec 文件,它看起来像这样(它是通过 ryanb 的 nifty_scaffold 生成器创建的):
require File.dirname(__FILE__) + '/../spec_helper'
describe Image do
it "should be valid" do
Image.new.should be_valid
end
end
这个测试失败了,我意识到是因为我的模型验证(即validates_attachment_presence)的问题。
我得到的错误是:
Errors: Photo file name must be set., Photo file size file size must be between 0 and 1048576 bytes., Photo content type is not included in the list
那么我如何告诉rspec在运行我的测试时上传一张照片呢?
我猜这可能与fixtures有关...也许不是。 我尝试过对它们进行一些调整,但没有成功。 就记录而言,我已经在我的fixtures文件夹中创建了一个名为images的文件夹,我想在我的测试中使用的两个文件分别为rails.png和grid.png)
我尝试了以下操作:
it "should be valid" do
image = Image.new :photo => fixture_file_upload('images/rails.png', 'image/png').should be_valid
# I've also tried adding stuff like this
#image.stub!(:has_attached_file).with(:photo).and_return( true )
#image.stub!(:save_attached_files).and_return true
#image.save.should be_true
end
但是Rspec抱怨无法识别“fixture_file_upload”...我打算去买那本Rspec书。 我在网上搜寻答案,但似乎找不到任何东西。如果我从模型中删除验证,我的测试数据库确实会填充一些数据,所以我知道它的某些部分可以正常工作。提前致谢,
编辑:
images.yml看起来像这样:
one:
name: MyString
description: MyString
two:
name: MyString
description: MyString
spec/factories.rb
文件中有这个:comment.pic File.new(RAILS_ROOT + '/spec/fixtures/images/rails.png')
但是我得到了RSpec错误:ActiveRecord::RecordInvalid in 'Comment should create a new instance given valid attributes'Validation failed: Pic file name image is required
- nnyby