I am trying to write a simple isolated test for a controller method in my Rails 4 application. The method takes an identifier from the query string, requests the Project model to give me some lines from the persistence level and make the result as JSON.
class ProjectsController < ApplicationController def projects_for_company render json: Project.for_company(params[:company_id]) end end
I am for_company with the bite of the for_company method. Here is the Im code trying:
require "rails_helper" describe ProjectsController do describe "GET #projects_for_company" do it "returns a JSON string of projects for a company" do dbl = class_double("Project") project = FactoryGirl.build_stubbed(:project) allow(dbl).to receive(:for_company).and_return([project]) get :projects_for_company expect(response.body).to eq([project].to_json) end end end
Since Ive omitted the for_company method, I expect the implementation of the method to be ignored. However, if my model looks like this:
class Project < ActiveRecord::Base def self.for_company(id) p "I should not be called" end end
... Then I see that I should not be called actually printed on the screen. What am I doing wrong?
source share