How to refactor primary key?

Currently our system uses the email of the user as a primary key, and other collections uses the email to find the user document.

A coworker suggested that we change the primary key to the Mongo ID, which seems reasonable. The issue is that when I started changing it most of the tests broke, and it is going to take a ton of work to update the all the tests.

So I have a few questions:

  1. Since this change has caused most of the tests to break, is there something about the tests that is brittle? Is there a way to build tests that would not be brittle in this way?

  2. Is there a way to “refactor the tests so that it is easy to make this change?”

  3. Part of the issue is that the new design requires a “profile” for each user. So, wherever I create a user using a factory (girl), I need to create a profile first. Is there an elegant way to run a before filter with Factory Girl to create another document?

  4. It is very slow and tedious to go through each test and figure out why it is not working and fix it. Most of the time I am just changing the test. This seems to be a very poor process and I don’t think it is good for the test codebase.

Can you paste one of the tests which breaks?

FactoryGirl has callbacks for this purpose (link to docs).

You could do

factory :user do
  after(:create) { this_runs_first }
  after(:create) { then_this }

although I would consider refactoring the views to not depending on a profile being created, possibly using the Null Object Pattern.

Agree. Sounds like the solution should lay somewhere in your factories file.