[rspec-users] Controller iterating through returned records and appending to each

Dylan Markow dmarkow at gmail.com
Tue Oct 16 11:01:44 EDT 2007

I have a controller that gets a list of employees (which has an "include =>
[:salaries, :incentives, :billablegoals, :reviews]"). I then need it to
iterate through each employee and determine their current active goal based
on the "effective date."

After playing around with it a bunch, I got the following to work. Only
problem is that if I remove the "@employees.each { |e|....." line, the test
still passes (which means the test isn't accurate). I had to stub out
"current_goal" and "current_goal=" because I kept getting "unexpected call"
errors on each.

Controller excerpt:
  def employee_report
    @employees = Employee.find_active_employees
    @employees.each { |e| e.current_goal = e.billablegoals.empty? ? "-" :
e.billablegoals.select { |bg| bg.effective_date <= Date.today }[0].goal }


require File.dirname(__FILE__) + '/../spec_helper.rb'

describe ManagementController, "GET /management/employee_report" do

  before(:each) do
    @bg1 = mock_model(Billablegoal, :effective_date => Date.today-3.months,
:goal => 50)
    @bg2 = mock_model(Billablegoal, :effective_date => Date.today + 1.month,
:goal => 75)
    @mock_employee = mock_model(Employee, :billablegoals => stub(Array,
:select => [@bg1], :empty? => false))
    @employees = [@mock_employee]
    @employees.each { |f| f.stub!(:current_goal=) }
    @employees.each { |f| f.stub!(:current_goal) }
    login # used to bypass the restful_authentication login_required filter

  def do_get
    get :employee_report

  it "should add the current billable goal to each returned employee" do
    @employees[0].should respond_to(:current_goal)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071016/72adeac6/attachment.html 

More information about the rspec-users mailing list