@[email protected]: 
Thank you for the idea, unfortunately it will not work, - as you can see, I 
have only 'firstname, lastname and total' that I can call on every element 
of the array.

array = Account.operations_by_client

  Account Load (0.0ms)  SELECT clients.firstname, clients.lastname, 
sum(operations.total) as total FROM "accounts" INNER JOIN "clients" ON "
clients"."id" = "accounts"."client_id" INNER JOIN "operations" ON 
"operations"."account_id" = "accounts"."id" GROUP BY clients.id, clients.f
irstname, clients.lastname ORDER BY clients.lastname
=> [#<Account >, #<Account >, #<Account >, #<Account >, #<Account >, 
#<Account >, #<Account >, #<Account >, #<Account >, #<Account >, #<Acco
unt >, #<Account >, #<Account >, #<Account >, #<Account >, #<Account >, 
#<Account >, #<Account >, #<Account >, #<Account >, #<Account >, #<A
ccount >, #<Account >, #<Account >, #<Account >, #<Account >, #<Account >, 
#<Account >, #<Account >, #<Account >, #<Account >, #<Account >,
#<Account >, #<Account >, #<Account >]
irb(main):003:0> ids = Account.operations_by_client.map(&:id)
  Account Load (0.0ms)  SELECT clients.firstname, clients.lastname, 
sum(operations.total) as total FROM "accounts" INNER JOIN "clients" ON "
clients"."id" = "accounts"."client_id" INNER JOIN "operations" ON 
"operations"."account_id" = "accounts"."id" GROUP BY clients.id, clients.f
irstname, clients.lastname ORDER BY clients.lastname
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, n
il, nil, nil, nil, nil, nil, nil, nil]
irb(main):004:0> ids = Account.operations_by_client.map(&:firstname)
  Account Load (0.0ms)  SELECT clients.firstname, clients.lastname, 
sum(operations.total) as total FROM "accounts" INNER JOIN "clients" ON "
clients"."id" = "accounts"."client_id" INNER JOIN "operations" ON 
"operations"."account_id" = "accounts"."id" GROUP BY clients.id, clients.f
irstname, clients.lastname ORDER BY clients.lastname
=> ["Alexis", "Ambre", "Clara", "Ambre", "Lisa", "Juliette", "Mathéo", 
"Gabriel", "Jeanne", "Célia", "Justine", "Adam", "Raphaël", "Alexis",
 "Mélissa", "Victor", "Anaïs", "Sarah", "Hugo", "Louna", "Nathan", "Maeva", 
"Carla", "Clémence", "Romain", "Lilou", "Baptiste", "Lena", "Nat
han", "Lucie", "Lucas", "Nathan", "Lucie", "Anaïs", "Ethan"]
irb(main):005:0> ids = Account.operations_by_client.map(&:total)
  Account Load (0.0ms)  SELECT clients.firstname, clients.lastname, 
sum(operations.total) as total FROM "accounts" INNER JOIN "clients" ON "
clients"."id" = "accounts"."client_id" INNER JOIN "operations" ON 
"operations"."account_id" = "accounts"."id" GROUP BY clients.id, clients.f
irstname, clients.lastname ORDER BY clients.lastname
=> [5380.2772339726025, 5184.132953424658, 5193.933681972603, 
5050.373567123288, 4409.951164931506, 4224.2900646575345, 
4446.3015346849315,
4332.022661260274, 4806.364291287671, 3986.5937578082194, 
4765.242442520548, 5055.00624, 4157.1036887671235, 5097.631978082191, 
5714.1788876
71233, 4214.344964383562, 3901.800284931507, 4564.252172273973, 
4262.283531178083, 4924.093100273973, 5125.269859945205, 5343.249759561644,
4753.427991452054, 5557.8009600000005, 4445.941435616438, 4168.82107090411, 
4890.077375123287, 6185.918728767123, 4734.1683349041095, 5046.5
6093369863, 4945.257031890411, 6050.124824547945, 4449.04845369863, 
5478.534525369863, 4807.532230136986]
irb(main):006:0>


On Tuesday, November 13, 2012 9:59:10 PM UTC+1, [email protected] wrote:
>
> In cases like this you can specify ids (or more data sufficient to make 
> the point), e.g.
>
> account = create(:operation)
> Account.operations_by_client.map(&:id).should eq [account.id]
>
> That make sense?
>
>
> On Tue, Nov 13, 2012 at 2:25 PM, Javix <[email protected] <javascript:>>wrote:
>
>> The quesion is not about the dificulty level, I knew that. I asked about 
>> the way to test the above sope method. So when calling it in the console, 
>> of course I'm getting an array, here is:
>>
>> ruby-1.9.3-p0 :001 > operations = Account.operations_by_client
>>   Account Load (26.8ms)  SELECT clients.firstname, clients.lastname, 
>> sum(operations.total) as total FROM "accounts" INNER JOIN "clients" ON 
>> "clients"."id" = "accounts"."client_id" INNER JOIN "operations" ON 
>> "operations"."account_id" = "accounts"."id" GROUP BY clients.id, 
>> clients.firstname, clients.lastname ORDER BY clients.lastname
>>  => [#<Account >, #<Account >, #<Account >, #<Account >, #<Account >, 
>> #<Account >, #<Account >, #<Account >, #<Account >, #<Account >, #<Account 
>> >, #<Account >, #<Account >, #<Account >, #<Account >, #<Account >, 
>> #<Account >, #<Account >, #<Account >, #<Account >, #<Account >, #<Account 
>> >, #<Account >, #<Account >, #<Account >, #<Account >, #<Account >, 
>> #<Account >, #<Account >, #<Account >, #<Account >, #<Account >, #<Account 
>> >, #<Account >, #<Account >] 
>> ruby-1.9.3-p0 :002 > 
>>
>> As you see, it is not a 'normal' array which we usually could test like 
>> that:
>>
>> describe 'GET #index' do
>>   it "displays an array of all the operations by client" do
>>     account = create(:operation) #will NOT work because the scope method 
>> result is not an array of Account objects
>>     get :index
>>     assigns(:operations).should == [account]
>>   end
>>
>>   it "renders the :index view" do 
>>     get :index
>>     response.should render_template :index
>>   end
>> end
>>
>>     
>> end
>>
>> Regards
>>
>>
>> On Tuesday, November 13, 2012 5:55:04 PM UTC+1, Alex Chaffee wrote:
>>>
>>>
>>> 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.p**aginate(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 rspec+un...@**
>>> googlegroups.com.
>>> To view this discussion on the web visit https://groups.google.com/d/**
>>> msg/rspec/-/pWkCaWOzo2cJ<https://groups.google.com/d/msg/rspec/-/pWkCaWOzo2cJ>
>>> .
>>> For more options, visit 
>>> https://groups.google.com/**groups/opt_out<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] <javascript:>
>> .
>> To unsubscribe from this group, send email to 
>> [email protected] <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msg/rspec/-/PUZR--nnFHAJ.
>> 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].
To view this discussion on the web visit 
https://groups.google.com/d/msg/rspec/-/BDB7BFQMSlsJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to