代码之家  ›  专栏  ›  技术社区  ›  bo-oz

如何对错误通知单进行rspec测试设置

  •  1
  • bo-oz  · 技术社区  · 8 年前

    通常我会创建如下控制器操作:

      def destroy
        if @account.destroy
          flash[:success] = "Account deleted!"
        else
          flash[:error] = "Could not delete account!"
        end
        redirect_to(:back)
      end
    

    显然,在大多数情况下,您不会遇到该请求的任何失败。我应该如何测试错误闪光灯是否设置好以防 @account.destroy 会以某种方式返回False吗?有没有办法嘲笑?

    1 回复  |  直到 8 年前
        1
  •  2
  •   AbM    8 年前

    如果您希望destroy方法总是返回 true ,最好有 @account.destroy! . 这样,如果 destroy 失败,您(作为应用程序所有者)将收到通知,以了解它(意外)失败的原因。

    def destroy
        @account.destroy!
        flash[:success] = "Account deleted!"
        redirect_to(:back)
    end
    

    但是要回答您最初的问题,您可以始终使用 allow_any_instance_of(Account).to receive(:destroy).and_return(false)