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]> 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]. > 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/-/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]. For more options, visit https://groups.google.com/groups/opt_out.
