Rspec规范控制器

问题描述 投票:0回答:1

我不知道,如何测试这样的代码,我想在没有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

结束

ruby-on-rails ruby rspec rspec-rails
1个回答
1
投票

这里有一些方法,我无法看到它们来自正确的存根,但你可能想要一些非常接近这个,测试对请求有意义的每个代码路径,以及它的副作用(发送电子邮件):

require "spec_helper"

describe "V1::ForgotPasswordsController" do
  let(:user) { User.create(email: "[email protected]") }

  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: "[email protected]" }
        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
© www.soinside.com 2019 - 2024. All rights reserved.