【问题标题】:Unable to update a user in Rspec with Devise and DeviseTokenAuth, although create and delete work fine无法使用 Devise 和 DeviseTokenAuth 更新 Rspec 中的用户,尽管创建和删除工作正常
【发布时间】:2017-05-28 11:21:27
【问题描述】:

在我的 Rails 5 应用程序中,我使用了这些宝石:

gem "devise", "~> 4.2.0"
gem "devise_token_auth"

我的用户模型:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable
  include DeviseTokenAuth::Concerns::User

我无法创建 Rspec 请求来更新用户。无论我尝试过什么,都失败了:

require "rails_helper"

RSpec.describe UsersController, type: :controller do
describe "PATCH #update" do
    context "with valid params" do
      it "updates the requested user" do
        u1 = create(:user)
        patch :update, params: { id: u1.to_param, user: attributes_for(:user, name: "name_new", email: "email_new@mail.com") },
              format: :json

        u1.reload
        expect(u1.name).to eq("name_new")
      end
    end

问题是创建和删除用户的测试运行良好。只有更新没有。

它在补丁请求期间发送新参数,但用户返回的参数始终是旧参数。错误是什么——我不知道,它只是说测试失败。再说一遍——只有一个用于更新。

我尝试过使用 PUT 和 POST —— 仍然失败。

【问题讨论】:

    标签: ruby-on-rails ruby rspec factory-bot ruby-on-rails-5


    【解决方案1】:

    这是我前段时间写的 Rspec 测试。

      it 'updates a user\'s email_address and password' do
        new_password = Faker::Internet.password(8)
        email_address = Faker::Internet.email
    
        request_params = {
          'email' => email_address,
          'name' => Faker::Name.name,
          'password' => new_password,
          'password_confirmation' => new_password
        }
    
        put '/myapp/v1/system/auth', request_params.to_json, subdomain_login(user.uid, password, account.subdomain)
        expect(response).to have_http_status(:ok)
    
        # Validating the password update with sign_in
        request_params = {
          'email' => email_address,
          'password' => new_password
        }
    
        post '/myapp/v1/system/auth/sign_in', request_params.to_json,
          { 'ACCEPT' => "application/json",
            'Content-Type' => "application/json" }
    
        expect(response).to have_http_status(:ok)
      end
    

    这是来自控制台的记录

    初始用户

    #<Myapp::V1::System::User id: "13153737-9efb-47e2-8ac9-85aa28befa2b", provider: "email", uid: "olin@schneiderbailey.name", name: nil, nickname: nil, image: nil, email: "olin@schneiderbailey.name", created_at: "2017-01-13 11:50:01", updated_at: "2017-01-13 11:50:01">
    

    更新参数

    {"email"=>"lisa@turner.info", "name"=>"Norberto Zulauf", "password"=>"OnT3Zv19YpZ", "password_confirmation"=>"OnT3Zv19YpZ"}
    

    响应正文

    "{\"status\":\"success\",\"data\":{\"id\":\"13153737-9efb-47e2-8ac9-85aa28befa2b\",\"email\":\"lisa@turner.info\",\"name\":\"Norberto Zulauf\",\"provider\":\"email\",\"uid\":\"lisa@turner.info\",\"nickname\":null,\"image\":null,\"created_at\":\"2017-01-13T11:50:01.175Z\",\"updated_at\":\"2017-01-13T11:50:03.436Z\"}}"
    

    请注意,注册控制器(也更新用户)被覆盖以满足特定要求。

    添加faker gem,如果需要尝试代码。 subdomain_login helper 参考这个帖子

    RSpec: Authenticating before test with devise_auth_token

    【讨论】:

      【解决方案2】:

      您需要passwordpassword_confirmation 默认更新设计用户。试试把它们传进去。

      此路由将更新现有用户的帐户设置。默认接受的参数是密码和密码确认,但这可以使用 devise_parameter_sanitizer 系统进行自定义。如果 config.check_current_password_before_update 设置为 :attributes,则在任何更新之前检查 current_password 参数,如果设置为 :password,则仅在请求更新用户密码时检查 current_password 参数。

      【讨论】:

        猜你喜欢
        • 2012-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-13
        • 2012-07-10
        • 1970-01-01
        • 2016-12-15
        相关资源
        最近更新 更多