Rspec让变量产生奇怪的结果

时间:2019-04-13 02:08:24

标签: ruby-on-rails rspec

我对RSpec有一个奇怪的问题,我不太了解。

这是我的port_stock_spec.rb文件:

# == Schema Information
#
# Table name: port_stocks
#
#  id                :bigint(8)        not null, primary key
#  portfolio_id      :integer
#  stock_id          :integer
#  volume            :integer
#  transaction_price :float
#  current_price     :float
#  percent_change    :float
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  current_value     :float
#  dollar_change     :float
#  total_spend       :float
#  transaction_date  :datetime
#  action            :integer
#  position          :integer          default("open")
#  ticker            :string
#  slug              :string
#

require 'rails_helper'

RSpec.describe PortStock, type: :model do
  let(:stock) { create(:stock, price: 10.00) }
  let(:portfolio) { create(:portfolio) }
  let(:port_stock_1) { create(:port_stock, stock: stock, portfolio: portfolio, transaction_price: stock.price, action: :buy, volume: 100) }

  context "associations" do
    it { should belong_to(:portfolio) }
    it { should belong_to (:stock) }
  end

  context "methods" do
    it "should accurately calculate the positive percent_change of the current PortStock" do
      port_stock_1.current_price = 20.00
      expect(port_stock_1.calculate_percent_change).to eql 100.00
    end

    it "should accurately calculate the negative percent_change of the current PortStock" do
      port_stock_1.current_price = 5.00
      expect(port_stock_1.calculate_percent_change).to eql(-50.00)
    end

    it "should accurately calculate the negative dollar_change of the current PortStock" do
      port_stock_1.current_price = 5.00
      port_stock_1.volume = 1000
      expect(port_stock_1.calculate_dollar_change).to eql (-5000.00)
    end

    # more specs that may or may no interact with the let variables.            

    it "should accurately calculate the portfolio's initial_dollar_value" do
      expect(portfolio.initial_dollar_value).to eql 1000.00
    end

结束

然后在我的portfolio.rb模型上使用以下方法:

  def calculate_portfolio_initial_dollar_value
    if self.portfolio.initial_dollar_value.nil?
      self.portfolio.initial_dollar_value = 0.0
    end
    self.portfolio.initial_dollar_value += (self.transaction_price * self.volume)
    self.portfolio.save!
  end

当我运行我的测试套件时,上一个测试会一直失败,而本不应该这样做:

Failures:

  1) PortStock methods should accurately calculate the portfolio's initial_dollar_value
     Failure/Error: expect(portfolio.initial_dollar_value).to eql 1000.00

       expected: 1000.0
            got: 798229.0

       (compared using eql?)
     # ./spec/models/port_stock_spec.rb:77:in `block (3 levels) in <top (required)>'

Finished in 5.05 seconds (files took 3.68 seconds to load)
29 examples, 1 failure, 19 pending

因此,我将binding.pry放在最近几次测试的it块中,当我检查portfolio.initial_dollar_value时,它会反复更改值。

[1] pry(#<RSpec::ExampleGroups::PortStock::Methods>)> portfolio
=> #<Portfolio:0x00007fcdc5c5db28
 id: 14,
 user_id: 7,
 current_dollar_value: 2864770.0,
 percent_change: 75.02,
 created_at: Sat, 13 Apr 2019 00:36:24 UTC +00:00,
 updated_at: Sat, 13 Apr 2019 00:36:24 UTC +00:00,
 num_winners: 2,
 num_losers: 7,
 initial_dollar_value: 860679.0,
 dollar_change: 92865.0>
[2] pry(#<RSpec::ExampleGroups::PortStock::Methods>)> port_stock_1.portfolio
=> #<Portfolio:0x00007fcdc5c5db28
 id: 14,
 user_id: 7,
 current_dollar_value: 150.0,
 percent_change: -85.0,
 created_at: Sat, 13 Apr 2019 00:36:24 UTC +00:00,
 updated_at: Sat, 13 Apr 2019 00:36:42 UTC +00:00,
 num_winners: 0,
 num_losers: 1,
 initial_dollar_value: 1000.0,
 dollar_change: -850.0>
[3] pry(#<RSpec::ExampleGroups::PortStock::Methods>)> portfolio
=> #<Portfolio:0x00007fcdc5c5db28
 id: 14,
 user_id: 7,
 current_dollar_value: 150.0,
 percent_change: -85.0,
 created_at: Sat, 13 Apr 2019 00:36:24 UTC +00:00,
 updated_at: Sat, 13 Apr 2019 00:36:42 UTC +00:00,
 num_winners: 0,
 num_losers: 1,
 initial_dollar_value: 1000.0,
 dollar_change: -850.0>
[4] pry(#<RSpec::ExampleGroups::PortStock::Methods>)> portfolio
=> #<Portfolio:0x00007fcdc5c5db28
 id: 14,
 user_id: 7,
 current_dollar_value: 150.0,
 percent_change: -85.0,
 created_at: Sat, 13 Apr 2019 00:36:24 UTC +00:00,
 updated_at: Sat, 13 Apr 2019 00:36:42 UTC +00:00,
 num_winners: 0,
 num_losers: 1,
 initial_dollar_value: 1000.0,
 dollar_change: -850.0>
[5] pry(#<RSpec::ExampleGroups::PortStock::Methods>)> 

我不明白为什么。

有想法吗?

编辑1

这是portfolio.rb工厂:

FactoryBot.define do
  factory :portfolio do
    user
    current_dollar_value { Faker::Number.number(7) }
    percent_change { Faker::Number.decimal(2) }
    num_winners { Faker::Number.number(1) }
    num_losers { Faker::Number.number(1) }
    initial_dollar_value { Faker::Number.number(6) }
    dollar_change { Faker::Number.number(5) }
  end
end

编辑2

我的port_stock.rb模型上有一个回调,它触发与portfolio_initial_dollar_value相关的方法:

after_save :calculate_portfolio_initial_dollar_value

还会影响投资组合其他方面的其他回调:

  after_save :update_portfolio_current_dollar_value
  after_save :update_portfolio_initial_dollar_value, if: (:total_spend_previously_changed? || :volume_previously_changed?)

  def update_portfolio_current_dollar_value
    self.portfolio.current_dollar_value = self.portfolio.port_stocks.open.map(&:current_value).sum
    self.portfolio.save!
  end

  def update_portfolio_initial_dollar_value
    self.portfolio.initial_dollar_value = self.portfolio.port_stocks.open.map { |ps| ps.volume * ps.transaction_price }.sum
    self.portfolio.save!
  end

编辑3

对于模型(port_stock.rb)和规格(port_stock_spec.rb)文件的完整版本,请选中out this gist。我不想用整个转储污染SO。

2 个答案:

答案 0 :(得分:4)

@grzekos指出,在执行stock测试期间,您永远不会调用port_stock_1it "should accurately calculate the portfolio's initial_dollar_value"

为什么?因为您使用了let,所以建立/创建了测试对象。 如果要始终设置/创建stockportfolioport_stock_1,则可以使用let!RSpec documentation)或使用before块像这样:

let(:stock) { create(:stock, price: 10.00) }
let(:portfolio) { create(:portfolio) }
let(:port_stock_1) { create(:port_stock, stock: stock, portfolio: portfolio, transaction_price: stock.price, action: :buy, volume: 100) }

before do
  stock
  portfolio
  port_stock_1
end

为什么在用pry进行删除期间看到不同的数字?

在您的第一个测试中,您调用了portfolio对象,该对象是使用FactoryBot创建的。工厂通过initial_dollar_valueFaker::Number.number(6)属性赋予了一个随机的6位数字。

在第二次测试中,您致电port_stock_1.portfolio。现在let(:port_stock_1)的块被求值。这将创建一个PortStock对象,该对象在其after_save方法中更新initial_dollar_value的{​​{1}}。

所有portfolioportfolio的子小节调用都不再更改port_stock_1.portfolio的值。

答案 1 :(得分:3)

好,所以失败的测试是:

    it "should accurately calculate the portfolio's initial_dollar_value" do
      expect(portfolio.initial_dollar_value).to eql 1000.00
    end

在这里,我看到您创建了portfolio对象,并且initial_dollar_value被设置为Faker::Number.number(6)(在工厂中)。为什么期望它等于1000.00?

运行此特定测试时,永远不会创建stockport_stock_1对象。引用Rspec let documentation

  

使用let定义记忆的辅助方法。该值将在   同一示例中没有多个调用。   请注意,let是惰性求值的:直到第一次才进行求值   调用它定义的方法。