2

当我尝试测试 mi 控制器时出现错误,我是 rspec 测试的新手,也许有些东西我不明白。

$rspec 规范/控制器/booking_controller_spec.rb

我收到以下错误:

没有路线匹配 {:controller=>"bookings", :action=>"/dashboard/index"}

我不明白为什么?所以如果能帮助我,我真的很感激一些帮助。

文件结构:

app/models/booking.rb
app/models/user.rb
app/models/role.rb
app/models/ability.rb   

app/controllers/bookings_controller.rb

app/views/bookings/index.html.erb
app/views/dashboard/index.html.erb

app/spec/controllers/bookings_controller_spec.rb

我的失败规格:

describe BookingsController do
    context 'as guest' do
    before(:each) do
        @user = User.new(:email => 'mail_admin@test.com',
                    :username => 'admin',
                    :password => 'password_admin',
                    :password_confirmation => 'password_admin')
        @user.save
        #when i save, with gem CanCan i assign a default role to @user
        #with the default role the user only can see the views/dashboard/index.html.erb
    end

    it 'should not render index template from bookings' do
        get :index
        response.should_not render_template(:index)
    end

    it 'should render index template from dashboard' do
        get '/dashboard/index'
        response.should render_template('index')
    end
end 
end

控制器:

class BookingsController < ApplicationController
load_and_authorize_resource

   def index
    ...      
   end

   def show
    ...
   end
end

我的模型:

class Booking < Activerecord::Base
paginates_per 20

   def 
    ...      
   end

   def 
    ...
   end
end

用户:

Class User < ActiveRecord::Base
  after_save :set_default_role
  rolify
  .
  .
  .
  .
  def set_default_role
    self.add_role :default
  end
end

角色:

class Role < ActiveRecord::Base
  ROLES = {"admin" => "Admin", "default" => "Default"}
  .
  .
  .
  . 
  scopify
end

能力:

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.has_role? :admin
      can :manage, :all
    elsif user.has_role? :data_consistency
      can :read, Booking
    end
  end
end
4

0 回答 0