Being no expert on Rails testing but perhaps I'd give a suggestion. When
something is hard to test, then perhaps you should extract that something into
an isolated scenario.
I personally would create a separate class for something like this, an
`OperationFetcher` class of some sort.
* It would take the account's id and the client's id, I believe those are the
dependencies.
* It is a result of data processing with the `select` and `order` commands.
Creating a class for this, IMO, is easier to test. No controller to mess you
around. You would have something like this:
describe OperationFetcher do
before do
// Create necessary account, client and operations here.
// Not sure what a convenient replacement of FactoryGirl could go here
end
let(:account_id) { 1 }
let(:client_id) { 2 }
subject { OperationFetcher.new account_id, client_id }
it "fetches operations by client" do
// your code here
end
end
This was not tested. But it should be a starting point. When you find something
really hard to test, then the design is probably not helping you. HTH!
--
José Mota
On Tuesday, November 13, 2012 at 8:46 AM, Javix 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]
> (mailto:[email protected]).
> To unsubscribe from this group, send email to
> [email protected]
> (mailto:[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.