mardi 29 septembre 2015

unit testing a sum in rails model

I want to create a test which tests if my method is working correctly.

class Contest < ActiveRecord::Base
  has_many :submissions 

  def tonnage
    self.submissions.sum(:tonnage)
  end

end

When I test (minitest) I get following error:

FAIL["test_0003_must have tonnage", #<Class:0x007fbcb9fac260>, 2015-09-29 21:23:47 +0200]
 test_0003_must have tonnage#Contest (1443554627.17s)
        --- expected
        +++ actual
        @@ -1 +1 @@
        -440
        +#<BigDecimal:7fbcbc9ac150,'0.0',9(27)>
        test/models/contest_test.rb:17:in `block (2 levels) in <top (required)>'

My test looks like this (minitest):

describe Contest do
  let(:contest) { Contest.create(name: "test",
                                 admin_id: 1,) }

  it "must have tonnage" do
    contest.tonnage.must_equal 440
  end


end

What does the test output mean (the failure) and what is the proper way to unit test this? I assume my method in my model is correct since it is working.

Aucun commentaire:

Enregistrer un commentaire