granite icon indicating copy to clipboard operation
granite copied to clipboard

Support for fixtures

Open spicybackend opened this issue 6 years ago • 4 comments

It would be great to have fixture support (similar to Rails' ActiveRecord::Fixtures) that allows for clean spec setup and scenarios.

spicybackend avatar Mar 21 '18 06:03 spicybackend

@yumoose thanks for the suggestion.

Until Granite/Amber gets a solution to this, I'd like to propose a solution to this problem which is reasonably easy to configure and extend, and doesn't obfuscate quite like fixtures do.

Create a class called Generate which contains a few class methods which can be used as helpers to quickly generate model instances:

class Generate
  def self.address
    Address.new.tap do |a|
      a.first_name = "Elwin"
      a.last_name = "Ransom"
      a.street_1 = "1 Malacandra Street"
      a.street_2 = "Hyoi's Water Dwelling n1"
      a.city = "Malacandra"

      a.country = Country.usa
      a.province = a.country.provinces.sample
      a.postal_code = '80019'
    end
  end
end

Require this in your test helper file, and then in a test you can do stuff like this:

# create an address with default test data
address = Generate.address

# insert an address into the database
Generate.address.save

# override attributes for an instance
address = Generate.address.tap do |a|
  a.country = "The Shire"
end

# create a helper method which overrides attributes and persists the object
def frodos_address
  Generate.address.tap do |a|
    a.country = "The Shire"
    a.first_name = "Frodo"
    a.last_name = "Baggins"
  end
end

# and use that in a spec
describe "addresses" do
  context "with a country" do
    it "does stuff" do
      frodos_address.should be_an Address
    end
  end
end

This is an application of a pattern I've used in Ruby projects many times and documented as a gist. I'm sure that a few of the metaprogrammy concepts won't translate directly to crystal, but with a little tinkering it wouldn't be hard to come up with a reasonable library.

robacarp avatar Mar 21 '18 16:03 robacarp

Once #253 gets merged it could be doable with JSON files. Read JSON/array of JSON from file and instantiate a model with given JSON values.

EDIT: Could also use YAML after #255 ;)

Blacksmoke16 avatar Jul 17 '18 01:07 Blacksmoke16

We should have a discussion on how this should work before we/I get too far into the weeds. Currently I have a simple YAML version working, similar to AR.

However, @robacarp had a different approach, similar to his example earlier. @yumoose @drujensen Think we can reach a good pattern on how to set this up?

Blacksmoke16 avatar Dec 07 '18 21:12 Blacksmoke16

I like @robacarp's generator approach, though this also requires supporting code for each model which can be a bit more of a hassle than static fixture sets for simple models that don't need too much configuration for their use in tests.

I'd personally love something similar to ActiveRecord using YAML files, but that's just what I'm used to. Both choices or a mix would be great for different situations as required.

spicybackend avatar Dec 08 '18 00:12 spicybackend