mercredi 3 mai 2017

test numericality in rails 5

I have the following table:

create_table "items", force: :cascade do |t|
    t.string   "name"
    t.float    "weight"
  end

and the model is defined as :

class Item < ApplicationRecord

validates :name, presence: true
validates :weight, presence: true, numericality: true

end

in test file I have the following:

require 'test_helper'

class ItemTest < ActiveSupport::TestCase
  def setup
    @item = Item.new(name: "item name", weight: 10)
  end

  test "test item is valid" do
    assert @item.valid? , 'item is not valid'
  end

  test "should accept integer"
    @item.weight = 12
    assert @item.valid?
  end

  test "should accept float"
    @item.weight = 12.5
    assert @item.valid?
  end

I can't pass the test because of weight although I can create and save new object in rails console.

Aucun commentaire:

Enregistrer un commentaire