rspec / rspec/rspec-activemodel-mocks

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

Open
#28 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
119
Forks
31
Avg merge
5h 47m
Merged PRs (30d)
2

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/rspec/active_model/mocks/mocks.rb at the referenced line, then reproduce under Rails 4.2.7.1 with eager_load=false while varying spec order. Compare the documented mock_model string behavior with the resulting constant lookup; done means the expected ActiveRecord-backed class behavior is established and covered for this scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.