gpt4 book ai didi

ruby-on-rails-3 - 使用 Rails 3 RC、Mongoid、Recaptcha 和 RSpec 触发 protected "reject"方法

转载 作者:行者123 更新时间:2023-12-01 04:16:28 24 4
gpt4 key购买 nike

请耐心等待,因为我对 rails(尤其是 rails 3)有点陌生,对此我有点困惑。基本上我想在我的应用程序中使用 RSpec 测试我的客户 Controller 。当我执行我的规范并尝试在我的 Controller 中对 create 操作执行帖子时,我收到此错误,我不知道为什么:

1) CustomersController as guest should render new template when executing create action with invalid model
Failure/Error: post :create, :customer => model
protected method `reject' called for #<Customer:0x0000010611c400>
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.beta.16/lib/mongoid/attributes.rb:23:in `method_missing'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0.rc/lib/active_model/mass_assignment_security/sanitizer.rb:6:in `sanitize'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activemodel-3.0.0.rc/lib/active_model/mass_assignment_security.rb:153:in `sanitize_for_mass_assignment'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.beta.16/lib/mongoid/attributes.rb:42:in `process'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.beta.16/lib/mongoid/document.rb:104:in `initialize'
# ./app/controllers/customers_controller.rb:11:in `new'
# ./app/controllers/customers_controller.rb:11:in `create'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/base.rb:136:in `process_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/rendering.rb:11:in `process_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/callbacks.rb:429:in `_run__4199577925444808436__process_action__3619821760006806352__callbacks'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/callbacks.rb:404:in `_run_process_action_callbacks'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/callbacks.rb:93:in `run_callbacks'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/callbacks.rb:17:in `process_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/notifications.rb:52:in `block in instrument'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.0.rc/lib/active_support/notifications.rb:52:in `instrument'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/rescue.rb:17:in `process_action'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/base.rb:105:in `process'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/abstract_controller/rendering.rb:40:in `process'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/metal/testing.rb:12:in `process_with_new_base_test'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/test_case.rb:404:in `process'
# /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.0.rc/lib/action_controller/test_case.rb:347:in `post'
# ./spec/controllers/customers_controller_spec.rb:14:in `block (2 levels) in <top (required)>'

现在我真的很抱歉,但这里有很多代码要遵循。首先是我的 gem 。
mongoid (2.0.0.beta.16)
mongoid-rspec (1.2.0)
...
rspec (2.0.0.beta.19)
rspec-core (2.0.0.beta.19)
rspec-expectations (2.0.0.beta.19)
rspec-mocks (2.0.0.beta.19)
rspec-rails (2.0.0.beta.19)
...
rails (3.0.0.rc)
...
recaptcha (0.2.3)

现在,我有我的 CustomersController定义如下:
class CustomersController < ApplicationController

respond_to :html
before_filter :authenticate_customer!, :except => [ :new, :create ]

def new
@customer = Customer.new
end

def create
@customer = Customer.new(params[:customer])
if verify_recaptcha(:model => @customer, :message => "Image verification failed. Please try again.") && @customer.save
flash[:notice] = "Your account has been created successfully"
end
respond_with @customer
end

def show
end
end

这是我的客户模型:
class Customer
include Mongoid::Document
devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable

field :first_name
field :last_name
field :email
field :roles, :type => Array, :default => [ 'User' ]

embeds_many :servers

validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :email
validates_presence_of :roles

validates_uniqueness_of :email

attr_accessible :first_name, :last_name, :email,
:password, :password_confirmation

def role?(role)
return !!self.roles.include?(role.to_s.camelize)
end

end

最后,这是我的规范:
describe CustomersController, "as guest" do
include Devise::TestHelpers

it "should render new template when executing new action" do
get :new
response.should render_template(:new)
end

it "should render new template when executing create action with invalid model" do
model = Customer.new(:first_name => "Test", :last_name => "user")
post :create, :customer => model
response.should render_template(:new)
end
end

最佳答案

事实证明,在我的每个测试中,我都必须传递一个元组而不是实际的模型对象。我改用 factory_girl 来创建我的模型对象,然后调用了 attributes_for每个模型对象上的方法。我将该结果传递给了 create行动,一切都按预期进行。

关于ruby-on-rails-3 - 使用 Rails 3 RC、Mongoid、Recaptcha 和 RSpec 触发 protected "reject"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3571461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com