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

ruby-on-rails-3 – Rspec,CanCan和Devise

发布时间:2020-12-17 03:19:23 所属栏目:百科 来源:网络整理
导读:我正在开始一个项目,我希望能够测试一切:) 我在CanCan和设计方面遇到了一些问题. 例如,我有一个控制器联系人.每个人都可以查看,每个人(除了被禁止的人)都可以建立联系. #app/controllers/contacts_controller.rbclass ContactsController ApplicationControl
我正在开始一个项目,我希望能够测试一切:)

我在CanCan和设计方面遇到了一些问题.

例如,我有一个控制器联系人.每个人都可以查看,每个人(除了被禁止的人)都可以建立联系.

#app/controllers/contacts_controller.rb
class ContactsController < ApplicationController
  load_and_authorize_resource

  def index
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(params[:contact])
    if @contact.save
      respond_to do |f|
        f.html { redirect_to root_path,:notice => 'Thanks'}
      end
    else
      respond_to do |f|
        f.html { render :action => :index }
      end
    end
  end
end

代码工作,但我不知道如何测试控制器.
我试过这个.如果我评论load_and_authorize_resource行,这是有效的.

#spec/controllers/contacts_controller_spec.rb
require 'spec_helper'

describe ContactsController do

  def mock_contact(stubs={})
    (@mock_ak_config ||= mock_model(Contact).as_null_object).tap do |contact|
      contact.stub(stubs) unless stubs.empty?
    end
  end

  before (:each) do
    #    @user = Factory.create(:user)
    #    sign_in @user
    #    @ability = Ability.new(@user)
    @ability = Object.new
    @ability.extend(CanCan::Ability)
    @controller.stubs(:current_ability).returns(@ability)
  end

  describe "GET index" do
    it "assigns a new contact as @contact" do
      @ability.can :read,Contact
      Contact.stub(:new) { mock_contact }
      get :index
      assigns(:contact).should be(mock_contact)
    end
  end

  describe "POST create" do

    describe "with valid params" do
      it "assigns a newly created contact as @contact" do
        @ability.can :create,Contact
        Contact.stub(:new).with({'these' => 'params'}) { mock_contact(:save => true) }
        post :create,:contact => {'these' => 'params'}
        assigns(:contact).should be(mock_contact)
      end

      it "redirects to the index of contacts" do
        @ability.can :create,Contact
        Contact.stub(:new) { mock_contact(:save => true) }
        post :create,:contact => {}
        response.should redirect_to(root_url)
      end
    end

    describe "with invalid params" do
      it "assigns a newly created but unsaved contact as @contact" do
        @ability.can :create,Contact
        Contact.stub(:new).with({'these' => 'params'}) { mock_contact(:save => false) }
        post :create,:contact => {'these' => 'params'}
        assigns(:contact).should be(mock_contact)
      end

      it "re-renders the 'new' template" do
        @ability.can :create,Contact
        Contact.stub(:new) { mock_contact(:save => false) }
        post :create,:contact => {}
        response.should render_template("index")
      end
    end

  end
end

但这些测试完全失败了….
我在网上什么都没看到……

(编辑:李大同)

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

    推荐文章
      热点阅读