Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does the "each" iterator method break rspec?
    primarykey
    data
    text
    <h2>Background</h2> <p>I'm attempting to test my models.</p> <h3>app/models/user.rb</h3> <pre><code>class User &lt; ActiveRecord::Base has_many :payor_transactions, class_name: 'Transaction', inverse_of: :payor, foreign_key: :payor_id has_many :payee_transactions, class_name: 'Transaction', inverse_of: :payee, foreign_key: :payee_id def transactions transactions = Transaction.where(["payor_id=? OR payee_id=?", self.id, self.id]) transactions end end </code></pre> <h3>app/models/transaction.rb</h3> <pre><code>class Transaction &lt; ActiveRecord::Base attr_accessor :user belongs_to :payor, class_name: 'User' belongs_to :payee, class_name: 'User' end </code></pre> <p>In the Transactions class, @user is an ephemeral object instance representing the user accessing the model.</p> <h3>spec/models/user_spec.rb</h3> <pre><code>require 'spec_helper' describe User do let(:user) { Factory(:user) } let(:user2) { Factory(:user) } let(:user3) { Factory(:user) } let(:transaction_user_user2) { Factory(:transaction, payor: user, payee: user2) } let(:transaction_user2_user) { Factory(:transaction, payor: user2, payee: user) } let(:transaction_user2_user3) { Factory(:transaction, payor: user2, payee: user3) } describe ".transactions" do it "should include payor and payee transactions but not 3rd party transactions" do user.transactions.should == [transaction_user_user2, transaction_user2_user] user2.transactions.should == [transaction_user_user2, transaction_user2_user, transaction_user2_user3] user3.transactions.should == [transaction_user2_user3] end end end </code></pre> <p>Using rspec 2.6.4, factory_girl 2.1.2, rails 3.1.0, ruby 1.9.2p290. As shown, the spec passes.</p> <h2>Problem</h2> <p>When I modify the <code>transactions</code> method in <strong>app/models/user.rb</strong> to iterate over the results such that it reads:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :payor_transactions, class_name: 'Transaction', inverse_of: :payor, foreign_key: :payor_id has_many :payee_transactions, class_name: 'Transaction', inverse_of: :payee, foreign_key: :payee_id def transactions transactions = Transaction.where(["payor_id=? OR payee_id=?", self.id, self.id]) transactions.each {|transaction| transaction.user = self} transactions end end </code></pre> <p>the method <code>transactions</code> now returns <code>[]</code> in rspec, <em>however it works perfectly in the app views</em>.</p> <p>Since Transaction.user is ephemeral (representing the user accessing the transaction) it must be set (if it exists) every time a Transaction is initialized or built from db records.</p> <p><strong>I'm at a loss for where to begin to debug this.</strong></p> <p>All suggestions appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload