ruby-on-rails - Rspec 规格控制器
问题描述
我不知道如何测试这样的代码,我想在没有 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
结尾
解决方案
这里有一些方法我看不到它们从哪里来正确存根,但你可能想要一些非常接近这个的方法,它测试对请求重要的每个代码路径,以及它的副作用(发送电子邮件):
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
推荐阅读
- sql - MSSQL 为每条记录选择前 2 名
- javascript - 赛普拉斯拦截与 url 不匹配
- python - SQLAlchemy Marshmallow 返回空 JSON Flask
- python - 使用 Python 分割 JSON 文件的最简单方法
- javascript - 棘手的“for”循环的输出?
- c# - 如何创建将 JSON 列表对象作为参数并且有多个请求的 ASYNC POST API?如何在 Postman 中测试多请求?
- primeng - Prime ng 日历 UTC
- r - 添加悬停文本(R Plotly Map)
- c++ - 在固定大小的假设下,我可以有一个多态值向量吗?
- javascript - 如何分隔具有相同类但不同功能的 div 块