[英]How to test my destroy action with RSpec?
In my Rails app, if a user
wants to delete his own account he will first have to enter his password in my terminate
view: 在我的Rails应用程序中,如果
user
要删除自己的帐户,则他首先必须在我的terminate
视图中输入密码:
<%= form_for @user, :method => :delete do |f| %>
<%= f.label :password %><br/>
<%= f.password_field :password %>
<%= f.submit %>
<% end %>
This is my UsersController
: 这是我的
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
The problem is that I can't seem to find a way to test this with RSpec. 问题是我似乎找不到用RSpec进行测试的方法。
What I have is this: 我所拥有的是:
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
However, this gives me an error: 但是,这给了我一个错误:
ActionView::MissingTemplate:
Missing template users/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder]}. Searched in:
* "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007fa7f51310d8>"
Can anybody tell me what I'm missing here or suggest a better way to test this action? 谁能告诉我我在这里缺少什么,或建议一种更好的方法来测试此操作?
Thanks for any help. 谢谢你的帮助。
According to Rspec docs Views are stubbed by default
. 根据Rspec的文档
Views are stubbed by default
。 So, I think you should add controller.prepend_view_path 'users/destroy'
in test method. 因此,我认为您应该在测试方法中添加
controller.prepend_view_path 'users/destroy'
。
First wrong what I sow is: 我播种的第一个错误是:
In destroy action you have params[:user][:password]
but in test you give only params[:id]
and params[:password]
在销毁动作中,您具有
params[:user][:password]
但是在测试中,您仅提供params[:id]
和params[:password]
in controller you create @user
in before_filter
? 在控制器中,您在
before_filter
创建@user
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.