Ruby on rails 3 带有rspec的Rails错误:“0”;预期有效?要返回true,则返回false“;
我是rails新手,在完成Michael Hartl的Ruby on rails教程后,我正在尝试扩展一些功能。我首先改变了microposts,这样他们可以收集更多的数据 在运行rspec测试时,我无法理解为什么会出现此错误:Ruby on rails 3 带有rspec的Rails错误:“0”;预期有效?要返回true,则返回false“;,ruby-on-rails-3,rspec-rails,railstutorial.org,Ruby On Rails 3,Rspec Rails,Railstutorial.org,我是rails新手,在完成Michael Hartl的Ruby on rails教程后,我正在尝试扩展一些功能。我首先改变了microposts,这样他们可以收集更多的数据 在运行rspec测试时,我无法理解为什么会出现此错误: $ bundle exec rspec spec/models/micropost_spec.rb ...............F..... Failures: 1) Micropost Failure/Error: it { should be_
$ bundle exec rspec spec/models/micropost_spec.rb
...............F.....
Failures:
1) Micropost
Failure/Error: it { should be_valid }
expected valid? to return true, got false
# ./spec/models/micropost_spec.rb:46:in `block (2 levels) in <top (required)>'
Finished in 1.57 seconds
21 examples, 1 failure
Failed examples:
rspec ./spec/models/micropost_spec.rb:46 # Micropost
microspost.rb
require 'spec_helper'
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before { @micropost = user.microposts.build(content: "Lorem ipsum",
title: "This is a test title",
privacy: "1",
groups: "This is a test Group",
loc1T: "21 Bond St. Toronto, Ontario",
loc1Lat: "43.654653",
loc1Lon: "-79.377627",
loc2T: "21 Bond St. Toronto, Ontario",
loc2Lat: "43.654653",
loc2Lon: "-79.377627",
startTime: "Jan 1, 2000 12:01:01",
endTime: "Jan 2, 2000 12:01:01",
imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif")
puts @micropost.errors.messages
}
#before { @micropost = user.microposts.build(title: "This is a test title") }
#before { @micropost = user.microposts.build(privacy: "1") }
#before { @micropost = user.microposts.build(groups: "This is a test Group") }
#before { @micropost = user.microposts.build(loc1T: "21 Bond St. Toronto, Ontario") }
#before { @micropost = user.microposts.build(loc1Lat: "43.654653") }
#before { @micropost = user.microposts.build(loc1Lon: "-79.377627") }
#before { @micropost = user.microposts.build(loc2T: "21 Bond St. Toronto, Ontario") }
#before { @micropost = user.microposts.build(loc2Lat: "43.654653") }
#before { @micropost = user.microposts.build(loc2Lon: "-79.377627") }
#before { @micropost = user.microposts.build(startTime: "Jan 1, 2000 12:01:01") }
#before { @micropost = user.microposts.build(endTime: "Jan 2, 2000 12:01:01") }
#before { @micropost = user.microposts.build(imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif") }
subject { @micropost }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
it { should respond_to(:title) }
it { should respond_to(:privacy) }
it { should respond_to(:groups) }
it { should respond_to(:loc1T) }
it { should respond_to(:loc1Lat) }
it { should respond_to(:loc1Lon) }
it { should respond_to(:loc2T) }
it { should respond_to(:loc2Lat) }
it { should respond_to(:loc2Lon) }
it { should respond_to(:startTime) }
it { should respond_to(:endTime) }
it { should respond_to(:imageURL) }
its(:user) { should == user }
it { should be_valid }
describe "accessible attributes" do
it "should not allow access to user_id" do
expect do
Micropost.new(user_id: user.id)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end
describe "with blank content" do
before { @micropost.content = " " }
it { should_not be_valid }
end
describe "with content that is too long" do
before { @micropost.content = "a" * 141 }
it { should_not be_valid }
end
end
class Micropost < ActiveRecord::Base
attr_accessible :content, :title,:privacy,:groups,:loc1T,:loc1Lat,:loc1Lon,:loc2T,:loc2Lat,:loc2Lon,:startTime,:endTime,:imageURL
belongs_to :user
validates :user_id, presence: true
validates :title, presence: true
validates :privacy, presence: true
validates :groups, presence: true
validates :loc1T, presence: true
validates :loc1Lat, presence: true
validates :loc1Lon, presence: true
validates :loc2T, presence: true
validates :loc2Lat, presence: true
validates :loc2Lon, presence: true
validates :startTime, presence: true
validates :endTime, presence: true
validates :imageURL, presence: true
validates :content, presence: true, length: { maximum: 140 }
default_scope order: 'microposts.created_at DESC'
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
end
FactoryGirl.define do
factory :user do
#name "Michael Hartl"
#email "michael@example.com"
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
factory :micropost do
content "Lorem ipsum"
title "This is a test title"
privacy "1"
groups "This is a test Group"
loc1T "21 Bond St. Toronto, Ontario"
loc1Lat "43.654653"
loc1Lon "-79.377627"
loc2T "21 Bond St. Toronto, Ontario"
loc2Lat "43.654653"
loc2Lon "-79.377627"
startTime "Jan 1, 2000 12:01:01"
endTime "Jan 2, 2000 12:01:01"
imageURL "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif"
user
end
end
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before { @micropost = user.microposts.build(content: "Lorem ipsum",
title: "This is a test title",
privacy: "1",
groups: "This is a test Group",
loc1T: "21 Bond St. Toronto, Ontario",
loc1Lat: "43.654653",
loc1Lon: "-79.377627",
loc2T: "21 Bond St. Toronto, Ontario",
loc2Lat: "43.654653",
loc2Lon: "-79.377627",
startTime: "Jan 1, 2000 12:01:01",
endTime: "Jan 2, 2000 12:01:01",
imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif")
puts @micropost.errors.messages
}
我不确定您是否还需要我发布其他内容。它无效,因为您的模型对象未通过验证。在您的
before
块中,在创建@micropost
后,添加此行以查看哪些验证失败
puts @micropost.errors.messages
将出现失败的验证(字段和错误消息)散列。修复这些,然后您的对象将是有效的。一些早期的评论者对从何处着手帮助解决问题提出了建议。这消除了错误 micropost\u规范rb
require 'spec_helper'
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before { @micropost = user.microposts.build(content: "Lorem ipsum",
title: "This is a test title",
privacy: "1",
groups: "This is a test Group",
loc1T: "21 Bond St. Toronto, Ontario",
loc1Lat: "43.654653",
loc1Lon: "-79.377627",
loc2T: "21 Bond St. Toronto, Ontario",
loc2Lat: "43.654653",
loc2Lon: "-79.377627",
startTime: "Jan 1, 2000 12:01:01",
endTime: "Jan 2, 2000 12:01:01",
imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif")
puts @micropost.errors.messages
}
#before { @micropost = user.microposts.build(title: "This is a test title") }
#before { @micropost = user.microposts.build(privacy: "1") }
#before { @micropost = user.microposts.build(groups: "This is a test Group") }
#before { @micropost = user.microposts.build(loc1T: "21 Bond St. Toronto, Ontario") }
#before { @micropost = user.microposts.build(loc1Lat: "43.654653") }
#before { @micropost = user.microposts.build(loc1Lon: "-79.377627") }
#before { @micropost = user.microposts.build(loc2T: "21 Bond St. Toronto, Ontario") }
#before { @micropost = user.microposts.build(loc2Lat: "43.654653") }
#before { @micropost = user.microposts.build(loc2Lon: "-79.377627") }
#before { @micropost = user.microposts.build(startTime: "Jan 1, 2000 12:01:01") }
#before { @micropost = user.microposts.build(endTime: "Jan 2, 2000 12:01:01") }
#before { @micropost = user.microposts.build(imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif") }
subject { @micropost }
it { should respond_to(:content) }
it { should respond_to(:user_id) }
it { should respond_to(:user) }
it { should respond_to(:title) }
it { should respond_to(:privacy) }
it { should respond_to(:groups) }
it { should respond_to(:loc1T) }
it { should respond_to(:loc1Lat) }
it { should respond_to(:loc1Lon) }
it { should respond_to(:loc2T) }
it { should respond_to(:loc2Lat) }
it { should respond_to(:loc2Lon) }
it { should respond_to(:startTime) }
it { should respond_to(:endTime) }
it { should respond_to(:imageURL) }
its(:user) { should == user }
it { should be_valid }
describe "accessible attributes" do
it "should not allow access to user_id" do
expect do
Micropost.new(user_id: user.id)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
describe "when user_id is not present" do
before { @micropost.user_id = nil }
it { should_not be_valid }
end
describe "with blank content" do
before { @micropost.content = " " }
it { should_not be_valid }
end
describe "with content that is too long" do
before { @micropost.content = "a" * 141 }
it { should_not be_valid }
end
end
class Micropost < ActiveRecord::Base
attr_accessible :content, :title,:privacy,:groups,:loc1T,:loc1Lat,:loc1Lon,:loc2T,:loc2Lat,:loc2Lon,:startTime,:endTime,:imageURL
belongs_to :user
validates :user_id, presence: true
validates :title, presence: true
validates :privacy, presence: true
validates :groups, presence: true
validates :loc1T, presence: true
validates :loc1Lat, presence: true
validates :loc1Lon, presence: true
validates :loc2T, presence: true
validates :loc2Lat, presence: true
validates :loc2Lon, presence: true
validates :startTime, presence: true
validates :endTime, presence: true
validates :imageURL, presence: true
validates :content, presence: true, length: { maximum: 140 }
default_scope order: 'microposts.created_at DESC'
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
end
FactoryGirl.define do
factory :user do
#name "Michael Hartl"
#email "michael@example.com"
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
factory :micropost do
content "Lorem ipsum"
title "This is a test title"
privacy "1"
groups "This is a test Group"
loc1T "21 Bond St. Toronto, Ontario"
loc1Lat "43.654653"
loc1Lon "-79.377627"
loc2T "21 Bond St. Toronto, Ontario"
loc2Lat "43.654653"
loc2Lon "-79.377627"
startTime "Jan 1, 2000 12:01:01"
endTime "Jan 2, 2000 12:01:01"
imageURL "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif"
user
end
end
describe Micropost do
let(:user) { FactoryGirl.create(:user) }
before { @micropost = user.microposts.build(content: "Lorem ipsum",
title: "This is a test title",
privacy: "1",
groups: "This is a test Group",
loc1T: "21 Bond St. Toronto, Ontario",
loc1Lat: "43.654653",
loc1Lon: "-79.377627",
loc2T: "21 Bond St. Toronto, Ontario",
loc2Lat: "43.654653",
loc2Lon: "-79.377627",
startTime: "Jan 1, 2000 12:01:01",
endTime: "Jan 2, 2000 12:01:01",
imageURL: "http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif")
puts @micropost.errors.messages
}
你可以输入micropost的值并检查它的值吗?我如何输入micropost的值并检查它的值?在一些测试用例中使用puts语句,然后运行bundle exec rspec spec,它会像…………micropost:blah.F……一样打印你的micropost,谢谢你的帮助,但我是rails新手。我不知道要添加puts语句的文件。我应该向文件中添加什么。谷歌搜索“puts语句”没有帮助。你在
之前的一系列块都在覆盖@micropost
,因此它们基本上是在相互撤销。这是我要做的第一件事。你的工厂里有相同的数据,所以你应该使用它。我这样做了,它通过了<代码>在{@micropost=user.micropost.build之前(内容:“Lorem ipsum”,标题:“这是一个测试标题”,隐私:“1”,组:“这是一个测试组”,loc1T:“安大略省多伦多市邦德街21号”,loc1Lat:“43.654653”,loc1Lon:-79.377627,loc2T:“安大略省多伦多市邦德街21号”,loc2Lat:“43.654653”,loc2Lon:“-79.377627”,开始时间:“2000年1月1日12:01:01”,结束时间:“2000年1月2日12:01:01”,图像URL:”http://i.cdn.turner.com/cnn/.e/img/3.0/global/header/hdr-main.gif)放置@micropost.errors.messages}
@livi1717检查test.log。日志中可能有更多关于对象无效原因的线索。