Skip to content

Instantly share code, notes, and snippets.

@phoenixbox
Created August 14, 2013 12:32
Show Gist options
  • Save phoenixbox/6230638 to your computer and use it in GitHub Desktop.
Save phoenixbox/6230638 to your computer and use it in GitHub Desktop.
Rspec matcher error, allow matcher wont accept two parameters
********* permission_spec.rb *********
require "spec_helper"
RSpec::Matchers.define :allow do |*args|
match do |permission|
permission.allow?(*args).should be_true
end
end
describe Permission, focus: true do
describe "as guest" do
subject { Permission.new(nil) }
binding.pry
it { should allow('topics','index') }
end
end
********* permission.rb *********
class Permission < Struct.new(:user)
def allow?(controller, action)
binding.pry
controller == "topics" && action == "index"
end
end
********* Application Contoller *********
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authorize
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
def current_permission
@current_permission ||= Permission.new(current_user)
end
def authorize
if !current_permission.allow?(params[:controller], params[:action])
redirect_to root_url, alert: "Not authorized to view this page!"
end
end
end
@redrick
Copy link

redrick commented Sep 12, 2013

hi there, I dont know if you want an answer or you just left this here for no reason, but today I was doing the #385 railscast and found this there

I dont seem to find answer for this in actual build of rspec and all that, but I did solve it with changing my Gemfile

on the bottom

gem "rspec-rails", :group => [:test, :development]
group :test do
gem "factory_girl_rails"
gem "capybara"
gem "guard-rspec", '> 2.0.0'
gem 'rspec', '
> 2.11.0'
end

and then delete lock file and bundle and you are ok again :)

(and I think you already noticed, but ryan uses build it has to be FactoryGirl.build)

I hope this helps :)
have a nice day :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment