RSpec验证失败,几乎没有解释

时间:2016-05-13 12:10:25

标签: ruby mongodb rspec

我已经创建了一个RSpec测试,只是测试我的模型是否对给定的信息有效,它应该是,但我的测试仍然失败。我希望有人能够明白为什么,因为我昨天整天盯着这一切。

我也使用MongoDB(不确定是否重要)。

模型/ stock.rb

$("#picture").on("click",".get_pic", function(e){
    var picture_id = $(this).attr('data-id');
    $("#picture").html("<div style=\"margin:50px auto;width:50px;\"><img src=\"loader.gif\" /></div>");
    $.post( "art.php", { pic: picture_id}, function( data ) {
        $("#picture").html( data );

        //you should give class name to p that's easy after to work
        $("table thead th:nth-child(2) p").html($(".artikel").html());

    });
    return false;
});

规格/ factories.rb

class Stock
  include Mongoid::Document
  field :symbol, type: String
  field :last_trade_price, type: Integer
  field :ask, type: Integer
  field :change, type: Integer
  field :change_percent, type: String
  field :market_cap, type: String
  field :avg_volume, type: Integer
  field :change_from_year_high, type: Integer
  field :change_from_year_low, type: Integer
  field :change_from_year_high_percent, type: Integer
  field :change_from_year_low_percent, type: Integer
  field :year_high, type: Integer
  field :year_low, type: Integer
  field :day_high, type: Integer
  field :day_low, type: Integer
  field :day_range, type: String
  field :ebitda, type: String
  field :eps_estimate_current_year, type: Integer
  field :eps_estimate_next_year, type: Integer
  field :eps_estimate_next_quarter, type: Integer


  validates :symbol, :last_trade_price, :ask, :change, :change_percent, :market_cap,
            :avg_volume, :change_from_year_high, :change_from_year_low, :change_from_year_high_percent,
            :change_from_year_low_percent, :year_high, :year_low, :day_high, :day_low, :day_range,
            :ebitda, :eps_estimate_current_year, :eps_estimate_next_year, :eps_estimate_next_quarter, presence: true

  validates :last_trade_price, :ask, :change, :avg_volume,
            :change_from_year_high, :change_from_year_low, :change_from_year_high_percent,
            :change_from_year_low_percent, :year_high, :year_low, :day_high, :day_low,
            :eps_estimate_current_year, :eps_estimate_next_year, :eps_estimate_next_quarter, numericality: true

  validates_uniqueness_of :symbol

end

规格/模型/ stock_spec.rb

FactoryGirl.define do
  factory :stock do
    symbol "AAPL"
    last_trade_price 92.51
    ask 92.78
    change -0.91
    change_percent "-0.91 - -0.97"
    market_cap "512.93B"
    avg_volume 37776500
    change_from_year_high -40.46
    change_from_year_low 0.66
    change_from_year_high_percent -30.43
    change_from_year_low_percent 0.72
    year_high 132.97
    year_low 91.85
    day_high 93.57
    day_low 92.46
    day_range "92.46 - 93.57"
    ebitda "82.79B"
    eps_estimate_current_year 8.29
    eps_estimate_next_year 9.15
    eps_estimate_next_quarter 1.67
  end
end

运行rspec测试的输出是:

describe Stock do
  let(:stock) { build(:stock) }

  it "should be valid if all information is provided" do
    expect(stock).to be_valid
  end
end

通过查看错误,似乎所有信息都内置在工厂测试对象中,因此我不确定为什么测试得到Failures: 1) Stock should be valid if all information is provided Failure/Error: expect(stock).to be_valid expected `#<Stock _id: 5734dd60b8066872f6000000, symbol: "AAPL", last_trade_price: 92, ask: 92, change: 0, change_percent: "-0.91 - -0.97", market_cap: "512.93B", avg_volume: 37776500, change_from_year_high: -40, change_from_year_low: 0, change_from_year_high_percent: -30, change_from_year_low_percent: 0, year_high: 132, year_low: 91, day_high: 93, day_low: 92, day_range: "92.46 - 93.57", ebitda: "82.79B", eps_estimate_current_year: 8, eps_estimate_next_year: 9, eps_estimate_next_quarter: 1>.valid?` to return true, got false # ./spec/models/stock_spec.rb:5:in `block (2 levels) in <top (required)>' Finished in 0.02311 seconds (files took 1.72 seconds to load) 1 examples, 1 failure Failed examples: rspec ./spec/models/stock_spec.rb:4 # Stock should be valid if all information is provided Randomized with seed 36574 而不是false它& #39;期待。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以通过修改规范来测试哪些字段产生错误:

describe Stock do
  let(:stock) { build(:stock) }

  it "should be valid if all information is provided" do
    #expect(stock).to be_valid
    stock.valid?
    expect(stock.errors.full_messages).to eq []
  end
end

然而即使这样的规格实际价值很小 - 你只是测试你的工厂有所有必需的字段。如果它没有,你会在其他规格中失败。

此外,如果您按类型对一组类似的验证进行分组,则可能需要使用简写方法,因为它更容易阅读:

  validates_presence_of :symbol, :last_trade_price, :ask, :change, :change_percent, :market_cap,
            :avg_volume, :change_from_year_high, :change_from_year_low, :change_from_year_high_percent,
            :change_from_year_low_percent, :year_high, :year_low, :day_high, :day_low, :day_range,
            :ebitda, :eps_estimate_current_year, :eps_estimate_next_year, :eps_estimate_next_quarter

在定义工厂时,您应该使用序列或计算属性来确保唯一字段是唯一的 - 否则,如果您从工厂创建多条记录,则验证将失败!

FactoryGirl.define do
  factory :stock do
    sequence :symbol do |n|
      "TEST-#{n}"
    end
    last_trade_price 92.51
    ask 92.78
    change -0.91
    change_percent "-0.91 - -0.97"
    market_cap "512.93B"
    avg_volume 37776500
    change_from_year_high -40.46
    change_from_year_low 0.66
    change_from_year_high_percent -30.43
    change_from_year_low_percent 0.72
    year_high 132.97
    year_low 91.85
    day_high 93.57
    day_low 92.46
    day_range "92.46 - 93.57"
    ebitda "82.79B"
    eps_estimate_current_year 8.29
    eps_estimate_next_year 9.15
    eps_estimate_next_quarter 1.67
  end
end

FFaker这样的宝石在这里真的很有帮助。有关详细信息,请参阅FactoryGirl docs

此外,您应该使用像database_cleaner这样的宝石(是的,它适用于mongoid)来清除规格之间的数据库 - 您的验证当前失败的原因是您从其他测试中获得了剩余的测试状态,这是影响结果。