1
votes

I am using rails 4.2.1 & ruby 2.2.1 in my appliation. So minitest is automatically added which is of version 5.1. There is no database in my application. With database I am able to test the model. How do I test the model without database?

I have created a user model:

class User
  include ActiveModel::Model
end

users.yml:

one:
  firstName: Avi
  email: [email protected]

user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase      
  test "the truth" do 
    user = users(:one)
    assert true
  end
end

Here I am getting the error: Undefined methods users. I am getting proper data if daabase exists. I even tried adding include ActiveModel::Lint::Tests still getting the same error. Can anyone help me on this?

Thanks

1

1 Answers

0
votes

The class ActiveSupport::TestCase expects that a database connection is active. You probably want to switch that to Minitest::Test, but that means that you can't use the users fixture method to retrieve a record from the database.

require 'test_helper'

class UserTest < Minitest::Test
  def test_sanity
    user = User.new
    user.first_name = "Avi"
    user.email = "[email protected]"
    assert user
  end
end