#ruby-on-rails #ruby #rspec #rspec-rails
#ruby-on-rails #ruby #rspec #rspec-rails
Вопрос:
Я понятия не имею, как протестировать подобный код, я хочу протестировать без Factory Bot, потому что это всего лишь пример:
class V1::ForgotPasswordsController < V1::BaseController
skip_before_action :authenticate_user!
expose(:verified_object) { verifier.verify(params[:token]) }
expose(:user) { User.find_by_email(params[:email] || verified_object.first) }
expose(:accounts) { User.by_email(verified_object.first) }
def create
if user
verify = verifier.generate([user.email, 1.hour.from_now])
UserMailer.forgot_password(user, verify).deliver_later
render json: { message: I18n.t('forgot_password.send_instructions') }, status: :accepted
else
render json: { message: I18n.t('forgot_password.error_email') }, status: :unprocessable_entity
end
end
def update
if token_valid?
return render json: { message: I18n.t('forgot_password.updated') }, status: :accepted unless update_passwords.include?(false)
render json: { message: I18n.t('forgot_password.error_password') }, status: :unprocessable_entity
else
render json: { message: I18n.t('forgot_password.error_token') }, status: 400
end
end
завершение
Комментарии:
1. Вот почему вы начинаете с тестов…
Ответ №1:
Здесь есть несколько методов, которые я не вижу, откуда они взялись, для правильной заглушки, но вы, вероятно, хотите что-то очень близкое к этому, которое проверяет каждый путь кода, значимый для запроса, и его побочные эффекты (отправка электронной почты):
require "spec_helper"
describe "V1::ForgotPasswordsController" do
let(:user) { User.create(email: "user@example.com") }
describe "#create" do
context "with a valid user" do
it "sends an email to the user and acknowledges the request as accepted" do
expect(UserMailer).to receive(:forgot_password).with(user, anything).and_call_original
post v1_create_path, params: { email: user.email }
expect(response).to be_accepted
expect(JSON.parse(response.body)[:message]).to eq I18n.t("forgot_password.send_instructions")
end
end
context "without a valid user" do
it "rejects the request" do
post v1_create_path, params: { email: "not_user@example.com" }
expect(response).to be_unprocessable_entity
expect(JSON.parse(response.body)[:message]).to eq I18n.t("forgot_password.error_email")
end
end
end
describe "#update" do
context "with valid token" do
before { allow_any_instance_of(V1::ForgotPasswordsController).to receive(:token_valid?).and_return(true) }
context "with update_passwords not including false" do
before { allow_any_instance_of(V1::ForgotPasswordsController).to recieve(:update_passwords).and_return([]) }
it "acknowledges the request as accepted" do
put v1_update_path(user), params: { email: user.email }
expect(response).to be_accepted
expect(JSON.parse(response.body)[:message]).to eq I18n.t("forgot_password.updated")
end
end
context "with update_passwords including false" do
before { allow_any_instance_of(V1::ForgotPasswordsController).to recieve(:update_passwords).and_return([false]) }
it "rejects the request" do
put v1_update_path(user), params: { email: user.email }
expect(response).to be_unprocessable_entity
expect(JSON.parse(response.body)[:message]).to eq I18n.t("forgot_password.error_password")
end
end
end
context "with an invalid token" do
before { allow_any_instance_of(V1::ForgotPasswordsController).to receive(:token_valid?).and_return(false) }
it "rejects the request as bad" do
put v1_update_path(user), params: { email: user.email }
expect(response).to be_bad_request
expect(JSON.parse(response.body)[:message]).to eq I18n.t("forgot_password.error_token")
end
end
end
end