У меня есть «обновленная запрашиваемая пользовательская часть» моего теста, которая терпит неудачу, и я не могу понять, почему.Ошибка математического ожидания ... Не понимаю, почему этот тест терпит неудачу (все отлично работает в браузере)
describe "PUT/PATCH #update_profile" do
context "with valid params" do
it "updates the requested user" do
user = create(:john_doe)
# Assuming there are no other users in the database, this
# specifies that the User created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
User.any_instance.should_receive(:update_profile).with({identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}})
put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}}}
end
it "assigns the requested user as @user" do
user = create(:john_doe)
put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}} }
expect(assigns(:user)).to eq(user)
end
it "redirects to the user" do
user = create(:john_doe)
put :update_profile, {:id => user.to_param, :user => {identity_attributes:{"last_name" => "Bidon", "first_name" => "Bidon", "dob" => "1970-07-15", "id"=>user.identity.id}}}
expect(response).to redirect_to foundry_users_url
end
end
2 других частей (правопреемников и перенаправлять) проходит нормально, и все работает, как ожидалось при тестировании в браузере.
Сообщение об ошибке "RSpec::Mocks::MockExpectationError: Exactly one instance should have received the following message(s) but didn't: update_profile"
EDIT: Я здесь добавить контроллер пользователей (я храню здесь только соответствующие части кода: создать действие обновления действия (для справки) и update_profile действие (которое вызывает спецификации потерпеть неудачу) . Помните, что только эта спецификация не удается, все другие работы, как ожидается, проблема только в том, как я написал тест.
пользователя has_one :identity
и accepts_nested_attributes_for :identity
class Foundry::UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :edit_profile, :update_profile, :destroy]
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to foundry_users_url, flash: {success: "User was successfully created."} }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @user.update(user_params_for_update)
format.html { redirect_to foundry_users_url, notice: 'Credentials were successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def update_profile
respond_to do |format|
if @user.update(user_params_for_update_profile)
format.html { redirect_to foundry_users_url, notice: 'Profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit_profile' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def user_params
# used only on creation
params.require(:user).permit(:email, identity_attributes: [:last_name, :first_name])
end
def user_params_for_update
# used only on 'regular' update action -- updates only credentials that are user's attributes
params.require(:user).permit(:email, :password, :password_confirmation)
end
def user_params_for_update_profile
# used only on update_profile action (later should have identity_attributes, addresses_attributes, and some others...)
params.require(:user).permit(identity_attributes: [:last_name, :first_name, :email_rescue, :dob, :bio, :gender, :id])
end
Я полагаю, что я делаю что-то wromg где-то, но я не могу увидеть, где и почему ...
Спасибо за вашу помощь
Что произойдет, если вы удалите '.with (...' часть из строки, начиная с 'User. any_instan ce.'? – DNNX
такая же вещь – phron
Не могли бы вы показать метод контроллера? – DNNX