Ruby on rails 包括重定向到的测试使用rspec失败,但应用程序按预期工作。迈克尔·哈特尔著作第9.6.6章(4.0节)

Ruby on rails 包括重定向到的测试使用rspec失败,但应用程序按预期工作。迈克尔·哈特尔著作第9.6.6章(4.0节),ruby-on-rails,ruby,rspec,ruby-on-rails-4,railstutorial.org,Ruby On Rails,Ruby,Rspec,Ruby On Rails 4,Railstutorial.org,我正试图解决Michael Hartl的书Rails v 4.0第9.6章中的一个问题6 尽管在rspec测试中一切看起来都很好: describe "should redirect a logged in user to root url if user tries to hit new in users controller" do let(:user) {FactoryGirl.create(:user)} before do sign_in user

我正试图解决Michael Hartl的书Rails v 4.0第9.6章中的一个问题6

尽管在rspec测试中一切看起来都很好:

  describe "should redirect a logged in user to root url if user tries to hit new in users controller" do
    let(:user) {FactoryGirl.create(:user)}
    before do
      sign_in user
      get new_user_path
    end

    specify{expect(response).to redirect_to(root_url)}
  end

  describe "should redirect a logged in user to root url if user tries to hit create in users controller" do
    let(:params) do {user: {name: "Tester", email: "test@example.com", password: "password",
                            password_confirmation: "password"}}
    end
    let(:user) {FactoryGirl.create(:user)}

    before do
      sign_in user
      post users_path, params
    end

    specify{expect(response).to redirect_to(root_url)}
  end
这是我的控制器代码段:

class UsersController < ApplicationController
before_action :is_signed_in, only: [:new, :create]
def new
  @user = User.new
end

def create
  @user = User.new(user_params)
  if @user.save
    sign_in @user
    flash[:success] = "Welcome to the Sample App!"
    redirect_to @user
  else
    render 'new'
  end
end

 private
  def is_signed_in
    redirect_to(root_url) if signed_in?
  end
end
但我的测试用例失败,出现以下错误:

Failures:

  1) Authentication signin with valid information should redirect a logged in user to root url if user tries to hit create in users controller
   Failure/Error: specify{expect(response).to redirect_to(root_url)}
   Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/users/2>.
   Expected "http://www.example.com/" to be === "http://www.example.com/users/2".
 # ./spec/requests/authentication_pages_spec.rb:69:in `block (5 levels) in <top (required)>'

  2) Authentication signin with valid information should redirect a logged in user to root url if user tries to hit new in users controller
 Failure/Error: specify{expect(response).to redirect_to(root_url)}
   Expected response to be a <redirect>, but was <200>
 # ./spec/requests/authentication_pages_spec.rb:55:in `block (5 levels) in <top (required)>'

Finished in 5.22 seconds
95 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:69 # Authentication signin with valid information should redirect a logged in user to root url if user tries to hit create in users controller
rspec ./spec/requests/authentication_pages_spec.rb:55 # Authentication signin with valid information should redirect a logged in user to root url if user tries to hit new in users controller

Randomized with seed 58509
编辑2 我正在用水豚来测试

Gemfile

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
  gem 'growl', '1.0.3'
  gem 'factory_girl_rails', '4.2.1'
  gem 'database_cleaner', '<1.1.0'
  gem "launchy", "2.3.0"
  gem 'cucumber-rails', '1.4.0', :require => false
end

我相信你错过了这一点:

no_capybara: true
所以它应该是这样的:

before do
  sign_in user, no_capybara: true
  get new_user_path
end
这是:

before do
  sign_in user, no_capybara: true
  post users_path, params
end

这是我和你的唯一区别。希望它能起作用

routes.rb
中如何定义我们的
root
?我建议添加像
put“signed-in:{is-signed\u-in}这样的临时内容
进入我们的
是登录的
方法。看看你的
登录的
助手方法是否真的登录了用户进行测试。你是使用水豚还是不使用水豚进行测试?另外,你介意分享你的
登录的吗?
方法吗?更新了问题。。!
no_capybara: true
before do
  sign_in user, no_capybara: true
  get new_user_path
end
before do
  sign_in user, no_capybara: true
  post users_path, params
end