在我的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>"有没有人能告诉我这里我遗漏了什么,或者建议一个更好的方法来测试这个动作?
谢谢你的帮助。
发布于 2013-10-22 17:06:44
根据Rspec docs Views are stubbed by default的说法。所以,我认为你应该在测试方法中添加controller.prepend_view_path 'users/destroy'。
发布于 2013-10-22 17:10:51
我提出的第一个错误是:
在销毁操作中,您有params[:user][:password],但在测试中,您只提供了params[:id]和params[:password]
在控制器中,您在before_filter中创建@user
https://stackoverflow.com/questions/19513219
复制相似问题