首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用RSpec测试此销毁操作?

如何使用RSpec测试此销毁操作?
EN

Stack Overflow用户
提问于 2013-11-06 19:15:45
回答 1查看 18.5K关注 0票数 8

在我的Rails应用程序中,如果user想要删除他自己的帐户,他首先必须在我的terminate视图中输入他的密码:

代码语言:javascript
复制
<%= form_for @user, :method => :delete do |f| %>

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

  <%= f.submit %>

<% end %>

这是我的UsersController

代码语言:javascript
复制
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来测试的方法。

我所拥有的是:

代码语言:javascript
复制
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

然而,这给了我一个错误:

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

有没有人能告诉我这里我遗漏了什么,或者建议一个更好的方法来测试这个动作?

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-07 04:13:50

好的,这是我的解决方案:

代码语言:javascript
复制
describe 'DELETE #destroy' do

  context "success" do

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

  end

end

我之前的before :each调用是无用的(这毕竟不是集成测试)。密码必须像这样传入:直到阅读this thread我才知道的:user => {:password => @user.password}

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19810635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档