mercredi 1 juillet 2015

Rails validation test not passing

In our Rails app, we have the following models:

class User < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :calendars, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

class Calendar < ActiveRecord::Base
  has_many :administrations, dependent: :destroy
  has_many :users, through: :administrations
end

We tried to validate the Administration model with the following administration_test.rb test file:

require 'test_helper'

class AdministrationTest < ActiveSupport::TestCase
  def setup
    @user = users(:noemie)
    @administration = Administration.new(user_id: @user.id, calendar_id: @calendar_id)
  end

  test "should be valid" do
    assert @administration.valid?
  end

  test "user id should be present" do
    @administration.user_id = nil
    assert_not @administration.valid?
  end

  test "calendar id should be present" do
    @administration.calendar_id = nil
    assert_not @administration.valid?
  end

end

When we run the test, we get the following results:

FAIL["test_calendar_id_should_be_present", AdministrationTest, 2015-06-30 07:24:58 -0700]
 test_calendar_id_should_be_present#AdministrationTest (1435674298.26s)
        Expected true to be nil or false
        test/models/administration_test.rb:21:in `block in <class:AdministrationTest>'

 FAIL["test_user_id_should_be_present", AdministrationTest, 2015-06-30 07:24:58 -0700]
 test_user_id_should_be_present#AdministrationTest (1435674298.27s)
        Expected true to be nil or false
        test/models/administration_test.rb:16:in `block in <class:AdministrationTest>'

We are kind of lost: is this the right way to right the test?

If no, how should we write it? If yes, how can we make it pass?

Aucun commentaire:

Enregistrer un commentaire