samedi 6 juin 2015

Correct use of ActiveModel with spec_helper

A new project with the RSpec 3.2 and Rails 4.2.

As a good citizen I'm trying to do the "right" thing with the new RSpec (disable monkey patching, do not infer the type of examples etc).

The problem I have is that I want to test this little class:

# app/models/session.rb

class Session # NOTE: This is a simple transient class, almost plain Ruby class
  include ActiveModel::Model
  attr_accessor :email, :password
end

with something like:

# spec/models/session_spec.rb
require 'spec_helper'

RSpec.describe Session, type: :model do
  it { should validate_presence_of :email }
  it { should validate_presence_of :password }
end

Obviously, when I run the spec I'm getting the uninitialized constant Session (NameError) which makes sense as the 'spec_helper' does not do anything to load that particular class.

I could have user the rails_helper instead but that is simply too much for that little class since it has nothing to do with rails at all.

Willing to do "the right" I'm puzzled with the following questions

  1. Does the Session spec even need to be marked with type: :model?
  2. Should I modify the spec_helper to change the $LOAD_PATH?
  3. If not, is there a way to autoload session.rb file but without loading up the whole Rails?
  4. Should the transient Session class be under app/models? I treat it as a model, it's just not persistent.

The easiest thing to do is of course to just use rails_helper, but for what I need to do that is unnecessary and I'd love to keep the spec file fast.

Aucun commentaire:

Enregistrer un commentaire