Rails教程Ch。 9练习6:预期的响应为<重定向>,但是<200> </redirect>

时间:2013-08-04 22:00:28

标签: ruby-on-rails railstutorial.org

我正在尝试编写测试和应用程序代码,以便在已尝试创建用户或访问新用户路径时重定向已登录到root_path的用户。

以下是我在user_pages_spec.rb中编写的测试:

describe "for signed in users" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      describe "using a 'new' action" do
        before { get new_user_path }
        specify { response.should redirect_to(root_path) }
      end

      describe "using a 'create' action" do
        before { post users_path }
        specify { response.should redirect_to(root_path) }
      end         
  end 

UsersController:

class UsersController < ApplicationController
  before_action :unsigned_in_user, only: [:create, :new]

  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
    # Before filters

    def user_params
      params.require(:user).permit(:name, :email, :password,
                               :password_confirmation)
    end

    def unsigned_in_user
      puts signed_in?
      redirect_to root_url, notice: "You are already signed in." unless !signed_in?
    end
end

puts signed_in?返回false。我假设这是问题,因为我希望它返回true。以下是使用rspec运行测试后的错误。任何帮助表示赞赏。

Failures:

  1) User pages for signed in users using a 'create' action 
     Failure/Error: before { post users_path }
     ActionController::ParameterMissing:
       param not found: user
     # ./app/controllers/users_controller.rb:52:in `user_params'
     # ./app/controllers/users_controller.rb:20:in `create'
     # ./spec/requests/user_pages_spec.rb:162:in `block (4 levels) in <top (required)>'

  2) User pages for signed in users using a 'new' action 
     Failure/Error: specify { response.should redirect_to(root_path) }
       Expected response to be a <redirect>, but was <200>
     # ./spec/requests/user_pages_spec.rb:158:in `block (4 levels) in <top (required)>'

在sessions_helper.rb文件中:

def signed_in?
    !current_user.nil?
end

在spec / support / utilities.rb中:

def sign_in(user, options={})
  if options[:no_capybara]
    # Sign in when not using Capybara.
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
  else
    visit signin_path
    fill_in "Email",    with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end
end

2 个答案:

答案 0 :(得分:4)

你能让你的考试通过吗?

如果你不是,我遇到了与你今天相同的问题,并且能够通过对测试进行两次更改来测试通过 - 在POST时传递user哈希,并使用no_capybara方法上的sign_in选项,因为getpost不是水豚方法,我认为如果我们从水豚切换到非水平,RSpec的行为并不像我们预期的那样-capybara方法在同一测试中。

describe "for signed-in users" do
  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user, no_capybara: true }

  describe "using a 'new' action" do
    before { get new_user_path }      
    specify { response.should redirect_to(root_path) }
  end

  describe "using a 'create' action" do
    before do
      @user_new = {name: "Example User", 
                   email: "user@example.com", 
                   password: "foobar", 
                   password_confirmation: "foobar"} 
      post users_path, user: @user_new 
    end

    specify { response.should redirect_to(root_path) }
  end
end   

答案 1 :(得分:3)

和najwa一样,但我使用了FactoryGirl 用户和Rails 属性方法来避免重复:

describe "using a 'create' action" do
  before { post users_path, user: user.attributes }

  specify { response.should redirect_to(root_path) }
end

帮助保持数据与测试代码分离。