A scope is a method that returns an array (basically). So create some test 
objects in the db (in all 3 tables), then call the method, and assert that the 
result contains what it should contain. 

With such a complicated query, you may need several tests (examples) to cover 
all the possible scenarios. 

On Nov 13, 2012, at 12:46 AM, Javix <[email protected]> wrote:

> I can't figure out how to test a scope method (highlighted in bold)  which 
> result includes data from 3 different tables:
> 
> class Account < ActiveRecord::Base
>   attr_accessible :acc_number, :client_id
>   belongs_to :client
>   has_many :operations, dependent: :destroy
>   scope :operations_by_client, joins(:client, 
> :operations).select('clients.firstname, clients.lastname, 
> sum(operations.total) as total').group('clients.id, clients.firstname, 
> clients.lastname').order('clients.lastname')
> end
> 
> 
> class Client < ActiveRecord::Base
>   has_many: accounts
> ...
> end
> 
> 
> class Operation < ActiveRecord::Base
>   belongs_to :account
> ...
> end
> 
> I get an array of Account objects as result (I hope so), so have no idea how 
> to use 'assigns' or smth other in controller spec for #index page:
> 
> 
> class OperationsController < ApplicationController
>   
>   def index    
>     @operations = Account.operations_by_client.paginate(page: params[:page])
>   end
> end
> 
> the same is for model spec, how is it possible to test the scope method? 
> Thanks
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "rspec" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/rspec/-/pWkCaWOzo2cJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to