Ruby on rails “测试载体持续返回”;图像可以';“不要空白”-错误

Ruby on rails “测试载体持续返回”;图像可以';“不要空白”-错误,ruby-on-rails,rspec,factory-bot,carrierwave,Ruby On Rails,Rspec,Factory Bot,Carrierwave,我一直在实现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 inclu

我一直在实现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
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
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
item.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
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
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
class项
我在spec/support/test_images文件夹中有一个名为google.png的图像


我是rails新手,非常感谢您的帮助

它{应该是有效的}

正在失败,因为(正如您所料)主题无效。你需要找出它无效的原因。试着这样做:

it "should be valid" do
  subject.valid?
  subject.errors.should be_empty
end
现在,示例将失败,但错误消息将更具描述性

另一种方法是添加到项目中。然后在要打开控制台的位置添加
binding.pry

it "should be valid" do
  subject.valid?
  binding.pry
  subject.errors.should be_empty
end

现在,您可以检查您的测试对象,找出验证是如何失败的。

我觉得自己很愚蠢。忘记附加图像,这显然导致验证失败

必须改变:

 before do
    @item = list.items.build(title: "Lorem ipsum")
    @item.valid?
    puts @item.errors.full_messages.join("\n")
 end
致:


谢谢你让我关注Pry-这是一个很棒的工具!