*更新:我将测试更改为:
require 'rails_helper'
describe 'User' do
context "validations" do
it {should validate_presence_of(:email)}
it {should validate_uniqueness_of(:email)}
end
end
现在正在接收:
*Failures:
1) User validations should require email to be set
Failure/Error: it {should validate_presence_of(:email)}
NoMethodError:
undefined method `email=' for "validations":String
# ./spec/models/user_spec.rb:5:in `block (3 levels) in <top (required)>'
2) User validations should require case sensitive unique value for email
Failure/Error: it {should validate_uniqueness_of(:email)}
NoMethodError:
undefined method `first' for String:Class
# ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00248 seconds (files took 1.23 seconds to load)
2 examples, 2 failures
Failed examples:
rspec ./spec/models/user_spec.rb:5 # User validations should require email to be set
rspec ./spec/models/user_spec.rb:6 # User validations should require case sensitive unique value for email*
您好!!使用rails 4和Rspec 3,我编写了以下测试
require 'rails_helper'
describe 'User' do
context 'validations' do
it { should validate_presence_of :email}
it "email must be present" do
expect {
user = User.new
user.save
}.not_to change{ User.count }
end
end
end
以下代码:
class User <ActiveRecord::Base
include ActiveModel::Model
attr_accessor :email
validates_presence_of :email, message: "Please provide your email address"
validates_uniqueness_of :email
def self.from_omniauth(auth)
where(email: auth.info.email).first_or_initialize.tap do |user|
user.uid = auth.uid
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.user_image = auth.info.image
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
端
并继续收到以下错误:
*User
validations
should require email to be set (FAILED - 1)
email must be present
Failures:
1) User validations should require email to be set
Failure/Error: it { should validate_presence_of :email}
NoMethodError:
undefined method `email=' for "validations":String
# ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.03091 seconds (files took 1.22 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:6 # User validations should require email to be set*
任何人都可以提供一些有关我可能出错的信息吗?提前谢谢。
答案 0 :(得分:0)
在规范的开头尝试describe User
。
说明: 当你写作
it {should validate_presence_of(:email)}
从主题调用 should
,默认情况下是描述类。您描述'User'
- 它不是一个类。
PS失败的规范告诉你方法email=
是从"validations"
调用的(这是你的上下文) - 这很奇怪并让我感到困惑。也许您应该直接设置subject
subject { User }
(详情为here)