我正在尝试遵循Rails 教程,但我的 rspec 失败,但在浏览器中,该应用程序可以根据需要运行。那是当我登录(在浏览器中)并转到“http://localhost:3000/users/1/edit”时,此页面将打开。本教程中所有先前的 rspecs 均已通过。
我正在使用 Rails '3.1.0.rc4' 并在 Windows 中工作。
失败的规范(来自 users_controller_spec.rb):
describe "GET 'edit" do
before(:each) do
@user = Factory(:user)
test_sign_in(@user)
end
it "should be successful" do
get :edit, :id => @user
response.should be_success
end
end
来自 spec_helper.rb 的 test_sign_in 代码:
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
def test_sign_in(user)
controller.sign_in(user)
end
end
工厂代码.rb
Factory.define :user do |user|
user.name "Mikhail"
user.email "mikleb@yandex.ru"
user.password "123456789a"
user.password_confirmation "123456789a"
end
来自控制台的文本:
Failures:
1) UsersController GET 'edit should be successful
Failure/Error: response.should be_success
expected success? to return true, got false
# ./spec/controllers/users_controller_spec.rb:111:in `block (3 levels) in <
top (required)>'
UPD:在“get :edit, :id => @user”之后,用户没有登录。但正如我在浏览器中写的,一切都按需要工作。这怎么可能?
这是 ApplicationControlelr 中包含的 SessionHelper:
module SessionsHelper
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@curent_user = user
end
def current_user
@current_user ||= user_from_remember_token
end
def deny_access
redirect_to signin_path, :notice => "Please sign in to access this page."
end
private
def user_from_remember_token
User.authenticate_with_salt(*remember_token)
end
def remember_token
cookies.signed[:remember_token] || [nil, nil]
end
end