我在运行rspec时得到的错误是: -
“未定义的方法 - authorize_manage_partner_links”
我在我的Gemfile中使用CANCAN gem进行授权,我使用的是Rails 3.2版和Ruby版1.9.2。
请建议做什么,因为我对rspec和rails完全不熟悉。我正在使用测试数据库,第一次导入数据库时,当我运行rspec时它显示成功,但之后它在运行相同的rspec文件时显示此错误。我正在使用FactoryGirl。我也在我的factoryrb文件中定义了所有内容。
这是我的控制器 -
require 'will_paginate/array'
class PartnerLinksController < ApplicationController
#load_and_authorize_resource
before_filter :authorize_manage_partner_links!
layout "backoffice"
def index
@partner_links = PartnerLink.all
if params[:sortby]
@partner_links = @partner_links.sort_by {|p| p.name} if params[:sortby] == "up"
@partner_links = @partner_links.sort_by {|p| p.name}.reverse if params[:sortby] == "down"
else
@partner_links = @partner_links.sort_by {|p| p.name}
end
total = @partner_links.size
@partner_links = @partner_links.paginate(:page => params[:page], :per_page => 20)
page = params[:page]
if !page
page = 1
end
render :locals => {:total => total, :page => page, :sortby => params[:sortby], :partner_link => @partner_link}
end
def new
@partner_link = PartnerLink.new
end
def edit
@partner_link = PartnerLink.find(params[:id])
end
def create
@partner_link = PartnerLink.new(params[:partner_link])
if @partner_link.save
redirect_to partner_links_url
else
render "new"
end
end
def update
@partner_link = PartnerLink.find(params[:id])
if @partner_link.update_attributes(params[:partner_link])
redirect_to partner_links_url
else
render "edit"
end
end
def destroy
@partner_link = PartnerLink.find(params[:id])
@partner_link.destroy
redirect_to partner_links_url
end
end
这是我正在使用的我的rspec控制器 -
require 'spec_helper'
describe PartnerLinksController do
before do
FactoryGirl.create(:partner_link)
role_permission
admin_role
user
currency
category
merchant
cart
deal
sign_in user
end
let(:role_permission) {FactoryGirl.create(:role_permission)}
let(:currency) {FactoryGirl.create(:currency)}
let(:merchant) {FactoryGirl.create(:merchant)}
let(:cart) {FactoryGirl.create(:cart)}
let(:deal) {FactoryGirl.create(:deal)}
let(:category) {FactoryGirl.create(:category)}
let(:authentication) {FactoryGirl.create(:authentication)}
let(:user) {FactoryGirl.create(:user)}
let(:admin_role) {FactoryGirl.create(:admin_role)}
describe "index" do
it "Should display all the partner links" do
get :index
end
end
describe "new" do
it "Should create all the partner links" do
get :new
end
end
describe "create" do
it "Should create new all the partner links" do
get :create
end
end
describe "update" do
it "Should update all the partner links" do
get :update
end
end
describe "destroy" do
it "Should delete all the partner links" do
get :destroy
end
end
end
应用程序/模型/ partner_link.rb -
class PartnerLink < ActiveRecord::Base
# attr_accessible :title, :body
validates_presence_of :name, :url, :description
end
这是我的Gemfile -
source 'http://rubygems.org'
ruby '1.9.2'
gem 'rails', '3.2.11'
gem 'authlogic'
gem "devise"
gem "devise-encryptable"
gem "devise-async"
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem "cancan"
gem 'activemerchant'
gem 'aws-sdk'
gem 'aws-s3'
gem 'paperclip', '~> 3.0'
gem 'oauth2'
#gem 'fastercsv' #no-longer required after ruby 1.9
gem 'pg'
#gem 'thin'
gem 'unicorn'
gem 'simple_form'
gem 'haml-rails' # Use HAML instead of ERB for templates
gem 'friendly_id'
gem 'will_paginate'
gem 'will_paginate-bootstrap'
gem "nifty-generators", :group => :development
#gem "pdfkit"
gem "pdfkit", "~> 0.5.0"
gem 'wkhtmltopdf-binary'
gem 'wicked_pdf'
gem 'sendgrid', "~> 1.0.1"
gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'bootstrap-datepicker-rails'
#gem 'roadie' #fixing email css
group :development, :test do
gem 'rspec-rails'
gem 'guard-rspec'
# Database Cleaner clears the database between tests. This done because we have
# to set config.use_transactional_fixtures = false in the spec_helper file.
# The use_transactional_fixtures value is set to false because Selenium test
# driver cannot access the test records created via database transactions.
gem 'database_cleaner'
end
group :test do
# gem 'factory_girl_rails'
gem 'capybara'
# As we’re using Capybara we can call the save_and_open_page method at any
# point and this will open the page in a browser so that we can take a look
# at it. This is enabled by Launchy.
# gem 'launchy'
# gem 'rspec-mocks'
end
gem "less-rails"
gem 'twitter-bootstrap-rails', '>= 2.0.3'
gem 'rb-readline', '~> 0.5.0', require: 'readline'
gem 'execjs'
gem 'therubyracer'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.2.3"
gem 'coffee-rails', "~> 3.2.1"
gem 'uglifier', '>= 1.0.3'
end
gem "mail", "~> 2.4.4"
gem 'jquery-rails'
#gem "mocha", :group => :test
gem 'gmaps4rails'
gem 'acts_as_xlsx'
gem "httparty"
gem 'newrelic_rpm'
gem 'font_assets'
gem 'paper_trail', '~> 3.0.6'
gem 'mailchimp-api', require: 'mailchimp'
gem 'rack-ssl-enforcer'
gem 'ckeditor'
gem "rails_best_practices"