Skip to content

mock_model("Transfer") creates persistent Class.new instead of loading ActiveRecord-backed class #28

Open
@rdunlop

Description

@rdunlop

In a rails 4.2.7.1 app, with eager_load = false (and rspec-rails 3.4.2 (though I have the same behavior on 3.5.0))

In my suite, I have a spec which runs mock_model("Transfer")

This sometimes works correctly, and sometimes does not, depending on the order of my specs.

I have tracked it down to the fact that sometimes, before this is run, some other spec has referenced Transfer, thus loading the class definition.

Problem If the spec which does mock_model("Transfer") runs first, any subsequent reference to Transfer will return a Class.new, instead of the ActiveRecord-backed definition.

I think I've tracked this to https://github.com/rspec/rspec-activemodel-mocks/blob/master/lib/rspec/active_model/mocks/mocks.rb#L91

I feel I have 2 options:

  1. set eager_load = true in test.rb
  2. reference mock_model(Transfer) instead of mock_model("Transfer")

I am reporting this here not because I don't have a way forward, but because I think that this behavior doesn't follow with the documentation https://github.com/rspec/rspec-activemodel-mocks#mock "A String representing a Class that extends ActiveModel::Naming"

Am I using this gem incorrectly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions