这是我在users_controller.rb中的创建方法。
我遇到了以下错误。我有什么问题吗? 我是Rspec测试的新手,如果能给出解决方法,将不胜感激。
谢谢。
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to users_url, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :password, :password_confirmation)
end
结束
这是我的Users_controllers_spec.rb文件。
require 'rails_helper'
RSpec.describe UsersController, type: :controller do
describe "Post /create" do
let(:user){build(:user)}
it "should create user" do
expect(User).to receive(:new).with({name: 'Naik', password: 'secret', password_confirmation: 'secret'}).and_return(User)
post :create, user: {name: 'Naik', password: 'secret', password_confirmation: 'secret'}
expect(flash[:success]).to eq("User was successfully created.")
expect(response).to redirect_to(users_path)
end
end
end
我遇到了以下错误。我有什么问题吗? 我是Rspec测试的新手,如果能给出解决方法,将不胜感激。
![Anyone has faced this error before ?](https://istack.dev59.com/PwF82.webp)
expect(User).to receive(:new).with({name: 'Naik', password: 'secret', password_confirmation: 'secret'}.with_indifferent_access)
- Deepeshparams = ActionController::Parameters.new( {name: 'Naik', password: 'secret', password_confirmation: 'secret'}) user_params = UsersController::UserParams.build(params) expect(user_params).to eq({name: 'Naik', password: 'secret', password_confirmation: 'secret'}.with_indifferent_access)
- Rajarshi Das