mercredi 25 octobre 2017

Rspec models not from codebase

I'm wondering if anyone could shed some light on the best way to test code that works with database models, but does not care what that model is.

Our app has a kind of synchronisation engine that keeps data on a remote server in sync with data on our system, and operates on a number of database models. It is also designed to work with multiple remote systems, and as such contains a number of internal data models which later get mapped to our actual database records.

As an example, we have something like this:

class RemoteResource < Syncable
  maps_to ::InternalDbModel
  ....
end

Here, the sync engine would pull data from any of the remote servers and convert it into a RemoteResource instance, and later this would get translated into an InternalDbModel for persistence.

Due to the generic design, the sync engine doesn't care what database models it is converting to. So long as they meet certain criteria, they are valid.

My question revolves around testing this sort of code. Due to time constraints, I've had to write tests that I simply don't like - the tests involve creating records of actual models used by the app, but I don't like this for a number of reasons:

  • Each test has to know about the model in order to create the records
  • If the model has other requirements, they must be fulfilled first, which is completely irrelevant for the test itself
  • It is not the model that I want to test

The syncable class is fine, as I am able to simply stub a class in the test and that works fine. But how can I do something similar for the active record model? Is there a way that I can create a kind of anonymous model class for these kind of tests? I will need to be able to persist the data as some of the sync engine process tests will require that data already exists, but i want this data to be completely independent of the main app itself.

I hope this makes sense

Aucun commentaire:

Enregistrer un commentaire