Rails教程Ch。 9练习6:预期的响应为,但
我正在尝试编写测试和应用程序代码,以便在已尝试创建用户或访问新用户路径时重定向已登录到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 ' 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 , but was # ./spec/requests/user_pages_spec.rb:158:in `block (4 levels) in '
在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
你能通过考试吗?
如果你不是,我遇到了与你今天相同的问题,并且能够通过对测试进行两次更改来测试通过 – 在no_capybara
时传递user
哈希,并在sign_in
方法上使用no_capybara
选项,因为get
和post
不是capybara方法,我认为如果我们在同一测试中从capybara切换到非capybara方法,RSpec的行为并不像我们预期的那样。
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
与najwa一样的答案,但我使用了Factory Rirl 属性方法的FactoryGirl 用户来避免重复:
describe "using a 'create' action" do before { post users_path, user: user.attributes } specify { response.should redirect_to(root_path) } end
有助于保持数据与测试代码分离。
- 未初始化的常量ApplicationRecord
- 如何将image_tag与bootstrap类一起使用:“img-responsive”? 第11章
- 在我的路线上出现一个奇怪的错误,“缺少必需的钥匙”栏杆4
- 无法运行rails服务器:`require’:无法加载此类文件–net / ssh(LoadError)
- $ bundle exec rake db:reset命令raise不能删除db / development.sqlite3
- 如何保持rspec测试DRY有很多“have_link”
- Rspec错误,“nil的未定义方法’downcase’:NilClass”
- 失败:在轨道上的ruby上预期0> = 1
- Rails教程:RSpec测试解耦