- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
好吧,我搜索了所有地方,似乎没有其他人遇到这种导致测试失败的“加密”方法的问题,尽管似乎很多其他人在第 7 章时遇到了一些困难。没有进一步的告别,
这是 Hartl 的 Chapter 7 的链接
我在用户模型文件中的代码,以及相应的规范文件,似乎与他所写的完全一致,但我仍然无法通过测试。错误?
Failures:
1) User should create a new instance given valid attributes
Failure/Error: User.Create!(@attr)
NoMethodError: undefined method 'encrypt' for #<User:asdf>
#./app/models/user.rb:22:in 'has_password?'
#./app/models/user.rb:28:in 'encrypt_password'
#./spec/models/user_spec.rb:15:in 'block (2 levels) in <top (required)>'
2) User should not allow duplicate email addresses
Failure/Error: User.Create!(@attr)
NoMethodError: undefined method 'encrypt' for #<User:asdf>
#./app/models/user.rb:22:in 'has_password?'
#./app/models/user.rb:28:in 'encrypt_password'
#./spec/models/user_spec.rb:15:in 'block (2 levels) in <top (required)>'
3) User should reject email addresses identical up to case
Failure/Error: User.Create!(@attr)
NoMethodError: undefined method 'encrypt' for #<User:asdf>
#./app/models/user.rb:22:in 'has_password?'
#./app/models/user.rb:28:in 'encrypt_password'
#./spec/models/user_spec.rb:15:in 'block (2 levels) in <top (required)>'
...
7) User has_password? method should be false if passwords do not match
Failure/Error: User.Create!(@attr)
NoMethodError: undefined method 'encrypt' for #<User:asdf>
#./app/models/user.rb:22:in 'has_password?'
#./app/models/user.rb:28:in 'encrypt_password'
#./spec/models/user_spec.rb:15:in 'block (3 levels) in <top (required)>'
所以每次测试我都会收到相同的错误消息,我会发疯地试图找出原因!
这是我的 user.rb:
require 'digest'
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
#automatically create the virtual attribute for 'password_confirmation'
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
before_save :encrypt_password
#returns true if the users password matches the submitted one
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
private
def encrypt_password
self.salt = make_salt unless has_password?(password)
self.encrypted_password = encrypt(password)
end
def encrypt_string
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end
和我的 user_spec.rb 文件:
require 'spec_helper'
require 'digest'
describe User do
before(:each) do
@attr = {
:name => "User Name",
:email => "example@email.com",
:password => "password",
:password_confirmation => "password"
}
end
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should require an email" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_user.should_not be_valid
end
it "should reject names that are too long" do
long_name = "a" * 51
long_name_user = User.new(@attr.merge(:name => long_name))
long_name_user.should_not be_valid
end
it "should accept valid email addresses" do
addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
addresses.each do |address|
valid_email_user = User.new(@attr.merge(:email => address))
valid_email_user.should be_valid
end
end
it "should reject invalid email addresses" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
addresses.each do |address|
invalid_email_user = User.new(@attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
end
it "should not allow duplicate email addresses" do
User.create!(@attr)
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
it "should reject email addresses identical up to case" do
upcased_email = @attr[:email].upcase
User.create!(@attr.merge(:email => upcased_email))
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
describe "password validations" do
it "should require a password" do
User.new(@attr.merge(:password => "", :password_confirmation => ""))
should_not be_valid
end
it "should require password to match the password confirmation" do
User.new(@attr.merge(:password_confirmation => "invalid"))
should_not be_valid
end
it "should reject short passwords" do
short = "a" * 5
hash = @attr.merge(:password => short, :password_confirmation => short)
User.new(hash).should_not be_valid
end
it "should reject long passwords" do
long = "a" * 41
hash = @attr.merge(:password => long, :password_confirmation => long)
User.new(hash).should_not be_valid
end
end
describe "password encryption" do
before(:each) do
@user = User.create!(@attr)
end
it "should have an encrypted password attribute" do
@user.should respond_to(:encrypted_password)
end
it "should not allow a blank encrypted password" do
@user.encrypted_password.should_not be_blank
end
end
describe "has_password? method" do
before(:each) do
@attr = User.create!(@attr)
end
it "should be true if the passwords match" do
@user.has_password?(@attr[:password]).should be_true
end
it "should be false if the passwords don't match" do
@user.has_password?("invalid").should be_false
end
end
end
如有任何帮助,我们将不胜感激。我倾注了其他人的问题,我的代码,并更改了各个方面以尝试让测试工作,但都无济于事。我希望这不是我还没有看到的真正愚蠢的东西。
最佳答案
你的错误在这里:
def encrypt_string
secure_hash("#{salt}--#{string}")
end
您在下面的encrypt_password 方法中调用了encrypt
,但是上面的方法名为encrypt_string
:
def encrypt_password
self.salt = make_salt unless has_password?(password)
self.encrypted_password = encrypt(password)
end
只需在方法定义中将 encrypt_string
更改为 encrypt
即可。
关于ruby-on-rails - RoR 教程,第 7 章 - rspec 测试失败 : NoMethodError 'encrypt' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8425301/
上下文: 我从存储库中提取了最新的代码,并尝试确保我将要推送的更改能够与该版本的代码一起使用。这是一个 Ruby on Rails 应用程序。另外值得注意的是,在运行我从网络上提取的主应用程序时,不会
让我们考虑以下代码: class Try CONST = xxx("42") private_class_method def self.xxx(str) str.to_i end end
我一直在寻找解决方案,但最相关的答案似乎规定使用 self. ,我已经在做;我无法用我可以找到我做错了什么的方式来表达我的搜索。我正在定义一个类,如下所示,但是当我尝试创建一个实例时,我得到了 NoM
我向 String 类添加了一个额外的方法。我想稍后使用此方法,但出现 no Method 错误。 class String def as_file_full_path NSSear
我尝试使用 ruby on Rails 请求数据库中的日期时间条目。这在 Linux 环境中工作得很好,但在我的 Windows 环境中我得到了 NoMethodError。调试时,我发现将日期时
我正在努力学习 ruby。我卡在第 26 课了。Here是文件的链接。这是错误所在的代码片段: five = 10 - 2 + 3 - 6 puts "This should be five: #
我不想在某个地方引发 NoMethodError 而只是针对某个类(例如 NilClass)。 例如 begin maybe_nil_maybe_not_nil = nil maybe_nil
我有一个正在编写的 sinatra 应用程序。我正在尝试编写一个页面来显示存储在数据库中的各个模式。使用 datamapper 我创建了一个类: require 'dm-core' require '
不确定是否有很多人熟悉 Ruby 的脚手架扩展,但我查看了他们的文档、论坛,甚至 Heroku 测试站点的源代码,但没有找到答案。 我制作了一个基本的 Sinatra 应用程序并按照 RDoc 的说明
我正在创建一个 Die 类,它具有输出骰子编号的功能。它已被命名为 printout 和 output,但由于 NoMethodError,函数继续失败。这是我的代码: class Die def
Whole source code here 我觉得我的程序流程有逻辑错误,返回NoMethodError 首先,一段导致错误的代码。 " /> #Error Text NoMethodError a
我正在尝试从我的一个规范助手中的模块访问方法 我将模块包含在测试助手中 module Support class RestHelper include Rest::Rest def
我在学习 Ruby 的时候正在做一个简单的 Pi Generator,但是我在 RubyMine 6.3.3 上一直遇到 NoMethodError,所以我决定用尽可能简单的方式创建一个新项目和新类,
我有一个这样的模块: module Prober def probe_invoke(type, data = {}) p = Probe.new({:probe_type => type.
我有一个 ruby 程序,它在特定的命名空间中创建部署、服务和 ing。当我想创建一个命名空间时,它会提示 NoMethodError。 这是代码: namespace = Kubeclient:
context 'with event_type is available create event' do let(:event_type) { EventType.where( name: '
我在 Ruby 中发现了一个奇怪的问题,我不太确定这是一个问题还是最近 Ruby 版本中引入的一个功能。 基本上当我们调用一个未定义的方法时,我们会得到一个 undefined method Ruby
我有以下 Ruby 代码,其中 BigClass 的每个实例创建一个具有 BigClass 实例的数组(直到最大深度)。 class BigClass # Increase this depend
我正在尝试 ror 教程,我遇到了以下代码行: index.html.erb: @players %> _player.html.erb: players_controller.rb: de
当我的 DeltaSynWorker 运行时,我收到 NoMethodErrors。这发生在作为当前工作应用程序的修订版构建的应用程序中。我不是最初的程序员,我是从 Java 背景开始的(我提到这一点
我是一名优秀的程序员,十分优秀!