I’m trying to unit test a model called UserModel. My RSpec code is:
# user_model.rb
require 'user_model'
describe UserModel do
result = UserModel.add("test","test")
result.should eq(1)
end
The line require 'user_model' raises an error:
ERROR : `./user_model.rb:1: uninitialized constant ActiveRecord (NameError)`
Is that line for importing the class UserModel?
If you don’t include your Rails environment in your test system then the results will be unpredictable. A typical test includes
test_helperat the very least, which by default loads in Rails and sets up the proper database connections.Every model when created with
rails generate modelwill come with a sample test you can customize.