在我的 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>"
谁能告诉我我在这里缺少什么或建议更好的方法来测试此操作?
谢谢你的帮助。