我尝试测试'cancan'gem。当我运行rspec时,在行
expect(ability).to be_able_to(:destroy, Project.new(:user => user))
shell显示错误
NoMethodError: undefined method `expect' for main:Object
这是ability_spec.rb
require 'spec_helper'
require "cancan/matchers"
describe Ability do
it "user has ability" do
user = FactoryGirl.create(:user)
ability = Ability.new(user)
expect(ability).to be_able_to(:destroy, Project.new(:user => user))
end
end
UPD
2.1.5 :004 > expect(ability).to be_able_to(:destroy, Project.new(:user => user))
NoMethodError: undefined method `expect' for main:Object
from (irb):4
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.5 :005 >
2UPD
weare138@weare138-P5K-SE:~/timonin$ bundle exec rspec spec/models/ability_spec.rb
/home/weare138/timonin/spec/models/ability_spec.rb:4:in `<top (required)>': uninitialized constant Ability (NameError)
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
from /home/weare138/.rvm/gems/ruby-2.1.5/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
from /home/weare138/.rvm/gems/ruby-2.1.5/bin/rspec:23:in `load'
from /home/weare138/.rvm/gems/ruby-2.1.5/bin/rspec:23:in `<main>'
from /home/weare138/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
from /home/weare138/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
如何解决?
抱歉我的英文不好
答案 0 :(得分:1)
您无法从控制台运行expect
命令(或者至少,这不是通常的方式)。
相反,要运行测试运行
bundle exec rspec path/to/ability_spec.rb
或者如果您想要运行所有测试:
bundle exec rspec .