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 8376975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:33:21+00:00 2026-06-09T15:33:21+00:00

I’m trying to test a controller to ensure that only an authorized party can

  • 0

I’m trying to test a controller to ensure that only an authorized party can view the correct child object using RSpec. I cant figure out what I’m doing wrong as I’m getting this error:

ActiveRecord::RecordInvalid: Validation failed: Company can't be blank

I have a Plan object and a Company object. The Store can have many plans (think of a pest control Company). I want to test that given a known scenario I can retrieve the plan fo the Company (assuming there is only one).

The Plan looks like this:

class Plan < ActiveRecord::Base
  before_save :default_values

  # Validation
  validates :amount, :presence => true
  validates :company, :presence => true

  # Plans belong to a particular company.
  belongs_to :company, :autosave => true    

  scope :find_all_plans_for_company, lambda {
    |company| where(:company_id => company.id)
  }
  # Other code ...

end 

The Company looks like this:

class Company < ActiveRecord::Base
  validates :name, :presence => true
  validates :phone1, :presence => true

  validates_format_of :phone1, :phone2,
                      :with => /^[\(\)0-9\- \+\.]{10,20}$/,
                      :message => "Invalid phone number, must be 10 digits. e.g. - 415-555-1212",
                      :allow_blank => true,
                      :allow_nil => true

  has_many :users
  has_many :plans

end

.. controller looks like this

def index
    @plans = Plan.find_all_plans_for_company(current_user.company)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @plans }
    end
  end

.. and my RSpec test looks like this (excuse me if its full of gimmickery, I’m just splunking around with it and cannot get it to work).

describe PlansController do

  def valid_attributes
    {
        :company_id => 1,
        :amount => 1000
    }
  end

  describe "GET index" do
    it "should return the Plans for which this users company has" do

      @company = mock_model(Company, :id => 1, :name => "Test Company", :phone1 => "555-121-1212")
      Company.stub(:find).with(@company.id).and_return(@company)

      controller.stub_chain(:current_user, :company).and_return(@company)

      plan = Plan.create! valid_attributes

      get :index, {}
      assigns(:plans).should eq([plan])
    end

    # Other tests ...
  end

end

The problem is, when I try this (or any of the crazy other variants I’ve tried) I get this error:

ActiveRecord::RecordInvalid: Validation failed: Company can't be blank

I’m not sure why this is happening as I thought the Company.stub call would handle this for me. But apparently not.

What am I missing here and what am I doing wrong? How can I get this test to pass?

  • 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-06-09T15:33:23+00:00Added an answer on June 9, 2026 at 3:33 pm

    Let’s peel back the layers on this spec, to make sure things make sense (and to make sure I understand what’s going on). First, what are you testing?

    it "should return the Plans for which this users company has" do
    
      ...
    
      assigns(:plans).should eq([plan])
    

    So you want to check that the plans associated with the company of the current user are assigned to @plans. We can stub or mock out everything else.

    Looking at the controller code, we have:

    def index
      @plans = Plan.find_all_plans_for_company(current_user.company)
    

    What do we need to get this to work, without hitting the database and without depending on the models?

    First of all, we want to get a mock company out of current_user.company. This is what these two lines in your spec code do:

      @company = mock_model(Company, :id => 1, :name => "Test Company", :phone1 => "555-121-1212")
      controller.stub_chain(:current_user, :company).and_return(@company)
    

    This will cause current_user.company to return the mock model @company. So far so good.

    Now to the class method find_all_plans_for_company. This is where I’m a bit confused. In your spec, you stub the find method on Company to return @company for id = 1.

    But really, wouldn’t it suffice just to do something like this in your controller code?:

      @plans = current_user.company.plans
    

    If you did it this way, then in your test you could just mock a plan, and then return it as the plans association for your mock company:

      @plan = mock_model(Plan)
      @company = mock_model(Company, :plans => [ @plan ])
      controller.stub_chain(:current_user, :company).and_return(@company)
    

    Then the assignment should work, and you don’t need to actually create any model or hit the database. You don’t even need to give your mock company an id or any other attributes, which anyway are irrelevant to the spec.

    Maybe I’m missing something here, if so please let me know.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.