我一直在实现carrierwave,它在浏览器中运行良好。但是,我的测试不断返回:
错误
1) Item
Failure/Error: it { should be_valid }
expected valid? to return true, got false
# ./spec/models/item_spec.rb:36:in `block (2 levels) in <top (required)>'
工厂.rb
include ActionDispatch::TestProcess
FactoryGirl.define do
sequence(:email) { |n| "User#{n}@example.com"}
factory :user do
name "John doe"
email
password "foobar"
password_confirmation "foobar"
end
factory :list do
name "Lorem ipsum"
user
end
factory :item do
image { fixture_file_upload(Rails.root.join('spec', 'support', 'test_images', 'google.png'), 'image/png') }
title "Shirt"
link "www.example.com"
list
end
end
item_spec.rb
require 'spec_helper'
describe Item do
let(:user) { FactoryGirl.create(:user) }
let(:list) { FactoryGirl.create(:list) }
before do
@item = list.items.build(title: "Lorem ipsum")
@item.valid?
puts @item.errors.full_messages.join("\n")
end
subject { @item }
it { should respond_to(:title) }
it { should respond_to(:list_id) }
it { should respond_to(:list) }
it { should respond_to(:image) }
it { should respond_to(:remote_image_url) }
its(:list) { should == list }
it { should be_valid }
describe "when list_id not present" do
before { @item.list_id = nil }
it { should_not be_valid }
end
describe "when image not present" do
before { @item.image = "" }
it { should_not be_valid }
end
describe "with blank title" do
before { @item.title = " " }
it { should_not be_valid }
end
describe "with title that is too long" do
before { @item.title = "a" * 141 }
it { should_not be_valid }
end
end
项目.rb
class Item < ActiveRecord::Base
attr_accessible :link, :list_id, :title, :image, :remote_image_url
belongs_to :list
mount_uploader :image, ImageUploader
validates :title, presence: true, length: { maximum: 140 }
validates :list_id, presence: true
validates_presence_of :image
end
我在 spec/support/test_images 文件夹中有一个名为 google.png 的图像。
我对 Rails 很陌生,非常感谢任何帮助!