Ruby on rails 3 为什么";每一个;迭代器方法中断rspec? 背景
我正在尝试测试我的模型 app/models/user.rb 使用RSPEC2.6.4、factory_girl 2.1.2、rails 3.1.0、ruby 1.9.2p290。如图所示,等级库通过 问题 当我修改app/models/user.rb中的Ruby on rails 3 为什么";每一个;迭代器方法中断rspec? 背景,ruby-on-rails-3,unit-testing,rspec2,ruby-1.9.2,Ruby On Rails 3,Unit Testing,Rspec2,Ruby 1.9.2,我正在尝试测试我的模型 app/models/user.rb 使用RSPEC2.6.4、factory_girl 2.1.2、rails 3.1.0、ruby 1.9.2p290。如图所示,等级库通过 问题 当我修改app/models/user.rb中的transactions方法来迭代结果时,它会显示: class User < ActiveRecord::Base has_many :payor_transactions, class_name: 'Transaction',
transactions
方法来迭代结果时,它会显示:
class User < 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
class用户
方法transactions
现在在rspec中返回[]
,但是它在应用程序视图中运行良好
由于Transaction.user是短暂的(表示访问事务的用户),因此每次初始化事务或根据db记录构建事务时都必须设置它(如果存在)
我不知道从哪里开始调试它。
感谢所有建议 我认为你的问题在于
let
是懒惰的。基本上,当在测试中调用transactions
方法时,甚至还没有创建事务。使用let代码>用于非惰性版本。有关更多详细信息,请参阅。我认为您的问题在于let
是懒惰的。基本上,当在测试中调用transactions
方法时,甚至还没有创建事务。使用let代码>用于非惰性版本。有关更多详细信息,请参见。您不能只返回付款人交易+收款人交易而不是手动选择它们吗?您不能只返回付款人交易+收款人交易而不是手动选择它们吗?按照@obrok的建议,我决定在其他测试中保留延迟加载let
的优势的解决方案是在测试用户事务之前触摸每个事务:
describe ".transactions" do
it "should include payor and payee transactions but not 3rd party transactions" do
[transaction_user_user2, transaction_user2_user, transaction_user2_user3].each do |transaction|
[transaction.payor_id, transaction.payee_id].each {|id| id.should_not be_nil }
end
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
根据@obrok的建议,我决定在其他测试中保留延迟加载let
的优势的解决方案是在测试用户#事务之前触摸每个事务:
describe ".transactions" do
it "should include payor and payee transactions but not 3rd party transactions" do
[transaction_user_user2, transaction_user2_user, transaction_user2_user3].each do |transaction|
[transaction.payor_id, transaction.payee_id].each {|id| id.should_not be_nil }
end
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
我仍然在学习rspec,所以我会假设这是我的rspec配置中的一个错误,而不是rspec本身的一个错误。my spec_helper.rb,以防万一:我仍然在学习rspec,所以我会假设这是我的rspec配置中的一个错误,而不是rspec本身的一个错误。my spec_helper.rb,以防万一:我打算添加分页,因此,我需要在db级别进行联合,但是transactions=self.payor\u transactions+self.paye\u transactions
会起作用,这很公平。分页总是让事情变得更困难。我打算添加分页,所以我需要在db级别进行联合,但是transactions=self.payor\u transactions+self.paye\u transactions
可以工作。这很公平。分页总是让事情变得更加困难。
class User < 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
describe ".transactions" do
it "should include payor and payee transactions but not 3rd party transactions" do
[transaction_user_user2, transaction_user2_user, transaction_user2_user3].each do |transaction|
[transaction.payor_id, transaction.payee_id].each {|id| id.should_not be_nil }
end
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