Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3426982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:46:53+00:00 2026-05-18T06:46:53+00:00

I am starting a project and i would like to be able to test

  • 0

I am starting a project and i would like to be able to test everything 🙂

And i have some problems with CanCan and devise.

For exemple, I have a controller Contacts. Everybody can view and everybody (excepts banned people) can create contact.

#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

The code work, but I don’t how to test the controller.
I tried this. This works if I comment the load_and_authorize_resource line.

#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

But these tests totally failed ….
I saw nothing on the web … 🙁
So, if you can advise me on the way i have to follow, i would be glad to ear you 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:46:53+00:00Added an answer on May 18, 2026 at 6:46 am

    CanCan does not call Contact.new(params[:contact]). Instead it calls contact.attributes = params[:contact] later after it has applied some initial attributes based on the current ability permissions.

    See Issue #176 for details on this and an alternative solution. I plan to get this fixed in CanCan version 1.5 if not sooner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just starting a project, I would like to have a small content manager
Starting a new project and would like to use one of the MVC framworks.
I am starting a project from scratch using Intersystems Cache. I would like to
In a project I'm starting I would like to give privileges to users based
I am starting a c++ project which I would like to compile equally well
I have a project in C++ that I would like to view the preprocessor
I'm starting a project with MVC 2 and would like to know if the
We are starting on a new project and would like to know if we
I'm a Silverlight newbie. I'm starting a new project and I would like to
I'm starting a project which I think would benefit from bindings (I've got a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.