我主要將我的應用程序從Rails 3.2移植到Rails 4.2。 (700次測試中約有2次仍然失敗)。從Rails 3.2移植到Rails 4.2 - 設計 - 註冊控制器繞過驗證
在之前的Rails堆棧,我用:
- 的Rails 3.2.18
- 紅寶石2.1.5
- 設計3.2.2
- RSpec的2.13.1
現在我正在使用
- 條的Rails 4.2.5
- 紅寶石2.2.4
- 設計3.5.3
- RSpec的2.14.1
我的用戶類看起來是這樣的:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :confirmable , :validatable
attr_accessible :name, :email, :password, :password_confirmation, :identity_url,
:remember_me, :terms
validates :terms, acceptance: { accept: 'yes' }
end
我的註冊控制器長相像這樣:
class RegistrationsController < Devise::RegistrationsController
def new
@title = "Sign up"
super
end
end
我對註冊控制器,它看起來像這樣一個RSpec測試:
describe "failure to accept terms" do
before(:each) do
@attr = { :name => "New User", :email => "[email protected]",
:password => "foobar", :password_confirmation => "foobar", :terms => "no" }
end
it "should not create a user" do
lambda do
@request.env["devise.mapping"] = Devise.mappings[:user]
post :create, :user => @attr
end.should_not change(User, :count)
#raise Exception, User.last.inspect
end
end
與以前的應用程序堆棧,測試通過。新的應用程序堆棧失敗。
1) RegistrationsController POST 'create' failure to accept terms should not create a user
Failure/Error: lambda do
count should not have changed, but did change from 0 to 1
隨着老棧或新的堆棧,如果我嘗試在Rails的控制檯下面,
irb(main) > User.all.count
=> 0
irb(main) > @attr = { :name => "New User", :email => "[email protected]", :password => "foobar", :password_confirmation => "foobar", :terms => "no" }
irb(main) > u = User.create(@attr)
irb(main) User.all.count
=> 0
irb(main) u.errors.messages
=> {:terms=>["must be accepted"]}
......不創建的用戶,並u.errors產量條款的驗證錯誤。這是應該發生的。
當我取消中考「引發異常」行,我得到如下:
#<User id: 2, name: nil, email: "[email protected]", created_at: "2016-01-22 18:13:35", updated_at: "2016-01-22 18:13:35", encrypted_password: "$2a$04$oySRVGtQQrbKkp9hmvHlIuA8kdpEASMkhnQ4rDuDC9L...", salt: nil, admin: false, reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmation_token: "_deYRJswq4yLLNrNNRAz", confirmed_at: nil, confirmation_sent_at: "2016-01-22 18:13:36", authentication_token: nil, unconfirmed_email: nil, terms: nil, identity_url: nil >
它看起來像用戶被創建,但如名稱關鍵數據和術語不分配。
長和短?使用Devise,用戶驗證全部使用Rails 3.2堆棧運行,並且沒有使用Rails 4.2堆棧運行。
有沒有辦法確保在使用基於Devise的註冊控制器時運行驗證?
這樣做。謝謝! –