加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用RSpec测试这种破坏行为?

发布时间:2020-12-16 19:21:39 所属栏目:百科 来源:网络整理
导读:在我的Rails应用程序中,如果用户想要删除自己的帐户,他首先必须在我的终止视图中输入他的密码: %= form_for @user,:method = :delete do |f| % %= f.label :password %br/ %= f.password_field :password % %= f.submit %% end % 这是我的UsersController:
在我的Rails应用程序中,如果用户想要删除自己的帐户,他首先必须在我的终止视图中输入他的密码:
<%= 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>"

任何人都可以告诉我我在这里缺少什么或建议更好的方法来测试这个动作吗?

谢谢你的帮助.

解决方法

好的,这是我的解决方案:
describe 'DELETE #destroy' do

  context "success" do

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

  end

end

之前:我之前的每次通话都是无用的(毕竟这不是集成测试).密码必须像这样传递:: user => {:password => @ user.password}在读到this thread之前我不知道.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读