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

Dylan Markow dmarkow at gmail.com
Tue Oct 16 11:24:37 EDT 2007


Okay I'm retarded. I added a helper method "def current_goal" to my Employee
model and can test it there, so I don't even need the @employees.each...
line.


Dylan Markow wrote:
> 
> 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 }
>   end
> 
> Spec:
> 
> 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) }
>     Employee.stub!(:find_active_employees).and_return(@employees)
>     login # used to bypass the restful_authentication login_required
> filter
>   end
> 
>   def do_get
>     get :employee_report
>   end
> 
>   it "should add the current billable goal to each returned employee" do
>     do_get
>     @employees[0].should respond_to(:current_goal)
>   end
> 
> end
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> 

-- 
View this message in context: http://www.nabble.com/Controller-iterating-through-returned-records-and-appending-to-each-tf4634810.html#a13235703
Sent from the rspec-users mailing list archive at Nabble.com.



More information about the rspec-users mailing list