Casecommons / with_model

Dynamically build an Active Record model (with table) within a test context
http://www.casebook.net
MIT License
166 stars 18 forks source link

Feature request: setup model once per per context, not per example #11

Closed artemave closed 11 years ago

artemave commented 11 years ago

At the moment, it seems to create table on every example (unless I am missing something).

It would be nice to have an option to do it once per with_table declaration, purely to make it faster.


Otherwise, thank you very much for a great gem. I also quite like how it adds the documentation aspect to the spec. Like here for instance:

describe DomainNameValidator do
  with_model :site do
    table do |t|
      t.string :host_name
    end

    model do
      validates :host_name, domain_name: true
    end
  end
  ...

with_model block shows what the table should have in order to use this custom validation and how to use it. Me like!

nertzy commented 11 years ago

The main reason I don't do that is to prevent test pollution.

But perhaps there could be an option to with_model and with_table that creates the table in before(:all) and tears it down after(:all).

If we were to keep the table between tests, would you expect that the table is empty at the beginning of each spec? How should it be emptied? Or should that be up to some other tool like DatabaseCleaner?

The options I can think of are:

  1. Force a transaction somehow.
  2. TRUNCATE the table in before(:each).
  3. DELETE FROM the table in before(:each).
  4. Nothing. It's up to the developer to provide a solution, or choose not to care.

I prefer 4 because it would match how other tables work.

artemave commented 11 years ago

Tests for existing models do not recreate schema between every test and it does not seem to cause pollution. I don't see why it would in case of with_model.

As for data cleansing, rspec wraps each example in transaction (or whatever user preference, such as DatabaseCleaner) so I wouldn't be surprised if it would just work out of the box in case of with_model before(:all). If not, tapping into rspec own cleansing behaviour somehow would seem to me like the best option.