运行 rspec 时出现以下错误:
bundle exec rspec spec/models/user_spec.rb
失败:
1) 用户名过长失败/错误:it { should_not be_vaild } NoMethodError: undefined method
vaild?' for #<User:0x007f8eebf0d6c0> # ./spec/models/user_spec.rb:39:in
block (3 levels) in '在 0.22196 秒内完成 6 个示例,1 个失败
失败的例子:
rspec ./spec/models/user_spec.rb:39 # 用户名过长
我的 user.rb 文件
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
attr_accessible :name, :email,
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true
end
我的 user_spec.rb
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_vaild }
end
end