在我的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 - 仍然失败。
答案 0 :(得分:0)
默认情况下,您需要password
和password_confirmation
来更新设备用户。尝试传递它们。
此路线将更新现有用户的帐户设置。默认接受的参数是password和password_confirmation,但可以使用devise_parameter_sanitizer系统进行自定义。如果config.check_current_password_before_update设置为:attributes,则在任何更新之前检查current_password参数,如果设置为:password,则仅当请求更新用户密码时才检查current_password参数。
答案 1 :(得分:0)
这是我写回来的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
帮助程序