lundi 21 novembre 2016

Dynamically destroying and creating data in Fixtures

I'm a little conceptually confused here, but it's not a purely conceptual question.

I think I'd be alright if I were able to delete and create "fixtures" on the fly, but I see no way to do this.

Let me back up a bit. I've inherited an app with a big fixture-based test suite. It was my initial inclination to perform various database operations in the test cases, to make sure my tests covered those situations. However I was advised that using SQL in tests is bad for performance, and I should use fixtures instead.

The question of fixtures vs factories is probably relevant, but I can't really go there right now - I need to use fixtures, and to figure out how to make my SQL-based test cases work with them.

Let me give an example, although this is a little pseudocodey

it "does some thing" do
  # this uses fixtures and is fine
  record_ids = Record.all.pluck :id
  expect(record_ids == [1,2,3]).to be true

  # here i want to delete some fixtures, how do I do this?
  Record.find(1).destroy
  expect(Record.all.pluck(:id).include?(1)).to be false

  # and how would I recreate the fixture?
  Record.create(id: 1)
  expect(Record.all.pluck(:id).include?(1)).to be true
end

I have a hunch that this is kind of against the spirit of fixtures, but what else are you supposed to do when your code calls Record.all? I don't see how I can change that result (to show different test cases) if I can't delete / create fixtures.

Aucun commentaire:

Enregistrer un commentaire