rspecを実行すると、次のエラーが発生します。
bundle exec rspec spec/models/user_spec.rb
失敗:
1)名前が長すぎる場合のユーザー失敗/エラー:it {should_not be_vaild} NoMethodError:未定義のメソッド
vaild?' for #<User:0x007f8eebf0d6c0> # ./spec/models/user_spec.rb:39:in
ブロック(3レベル)'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