簡體   English   中英

如何使用RSpec測試我的銷毀動作?

[英]How to test my destroy action with RSpec?

在我的Rails應用程序中,如果user要刪除自己的帳戶,則他首先必須在我的terminate視圖中輸入密碼:

<%= form_for @user, :method => :delete do |f| %>

  <%= f.label :password %><br/>
  <%= f.password_field :password %>

  <%= f.submit %>

<% end %>

這是我的UsersController

def terminate
  @user = User.find(params[:id])
  @title = "Terminate your account"
end

def destroy
  if @user.authenticate(params[:user][:password])
    @user.destroy
    flash[:success] = "Your account was terminated."
    redirect_to root_path
  else
    flash.now[:alert] = "Wrong password."
    render :terminate
  end
end

問題是我似乎找不到用RSpec進行測試的方法。

我所擁有的是:

describe 'DELETE #destroy' do

  before :each do
    @user = FactoryGirl.create(:user)
  end

  context "success" do

    it "deletes the user" do
      expect{ 
        delete :destroy, :id => @user, :password => "password"
      }.to change(User, :count).by(-1)
    end

  end

end

但是,這給了我一個錯誤:

ActionView::MissingTemplate:
Missing template users/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder]}. Searched in:
* "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007fa7f51310d8>"

誰能告訴我我在這里缺少什么,或建議一種更好的方法來測試此操作?

謝謝你的幫助。

根據Rspec的文檔 Views are stubbed by default 因此,我認為您應該在測試方法中添加controller.prepend_view_path 'users/destroy'

我播種的第一個錯誤是:

在銷毀動作中,您具有params[:user][:password]但是在測試中,您僅提供params[:id]params[:password]

在控制器中,您在before_filter創建@user嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM