Hosang Jeon
Hosang Jeon

Reputation: 1423

Minitest:Test in Ruby on Rails produces more assertions than I expect

I'm starting to use Minitest for doingi unit test on rails.

Here is my first test case for Product model.

require "test_helper"

class ProductTest < ActiveSupport::TestCase
  test "product price must be positive" do
    product = Product.new(title: "My Book Title",
                          description: "yyy",
                          image_url: "zzz.jpg")
    product.price = -1
    assert product.invalid?, "negative price of a product must be invalid."
    assert product.errors.has_key?(:price), "an invalid product must have an error"

    product.price = 0
    assert product.invalid?, "negative price of a product must be invalid."
    assert product.errors.has_key?(:price), "an invalid product must have an error"

    product.price = 1
    assert product.valid?, "positive price of a product must be valid."
    assert_empty product.errors, "a valid product must have no error"
  end
end

I've added 6 assertions for this test case. However, if I run this test case, the console output produces 7 assertions run.

$ rake test test/models/product_test.rb 


[DEPRECATION] Ahoy subscribers are deprecated
Run options: --seed 31334

# Running:

.

Finished in 0.260717s, 3.8356 runs/s, 23.0134 assertions/s.

1 runs, 7 assertions, 0 failures, 0 errors, 0 skips

Is there anyone who can help me to figure out the reason of mismatching the number of assertions?

Thanks in advance.

Upvotes: 2

Views: 428

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

Looking at the source, assert_empty indeed makes two assertions for the price of one.

def assert_empty obj, msg = nil
  msg = message(msg) { "Expected #{mu_pp(obj)} to be empty" }
  assert_respond_to obj, :empty?
  assert obj.empty?, msg
end

Upvotes: 5

Related Questions