revans / fixture_overlord

The Overlord of Fixtures

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Warning!!

Deprecated

Going to provide a rails-esque fixture-mock library

FixtureOverlord

  • This is an Alpha version and is currently under heavy development.

Build Status

Code Climate

Setup

require 'minitest/autorun'

class MiniTest::Unit::TestCase
  # include FixtureOverlord's Mixin
  include FixtureOverlord

  # add the fixture accessor that will define 1 method for each YAML file
  # within {spec,test}/fixtures.
  fixture_overloard :rule # set to :off to not define methods
end

How to use in your Tests

Mocks

class GameTest < MiniTest::Unit::TestCase

  # mock will create an OpenStruct Class with accessors
  # to reference the data.
  #
  # Assuming we have a fixture file named games.yml,
  # and inside that file, we have:
  #
  # tron:
  #   name:    'Tron'
  #   type:    'Video Game'
  #   rating:  'Gold'
  #
  #
  # e.g.
  #   OpenStruct.new(hashed_version_of_yaml_file)
  #
  def test_mock_game
    tron = games(:tron).mock

    assert_equal 'Tron',       tron.name
    assert_equal 'Video Game', tron.tyoe
    assert_equal 'Gold',       tron.rating
  end

end

Use as a plain old hash

class GameTest < MiniTest::Unit::TestCase

  # when a fixture is instantiated, it's a hash with super powers,
  # and it's keys are automatically symbolized.
  #
  # games.yml:
  #
  #   tron:
  #     name:    'Tron'
  #     type:    'Video Game'
  #     rating:  'Gold'
  #
  #
  def test_hash_attributes
    tron = games(:tron)

    assert_instance_of Hashish, tron
    assert_equal 'Tron',       tron[:name]
    assert_equal 'Video Game', tron[:type]
    assert_equal 'Gold',       tron[:rating]
  end

end

Remove Attributes

class GameTest < MiniTest::Unit::TestCase

  # removing attributes
  #
  # games.yml:
  #
  #   tron:
  #     name:    'Tron'
  #     type:    'Video Game'
  #     rating:  'Gold'
  #
  #
  def test_hash_attributes
    tron = games(:tron)

    tron.remove(:name)
    assert_nil tron[:name]
  end

end

Add Attributes

class GameTest < MiniTest::Unit::TestCase

  # adding attributes
  #
  # games.yml:
  #
  #   tron:
  #     name:    'Tron'
  #     type:    'Video Game'
  #     rating:  'Gold'
  #
  #
  def test_hash_attributes
    tron = games(:tron)

    tron.add(likes: 20)
    assert_equal 20, tron[:likes]

    tron = tron.mock
    assert_equal 20, tron.likes
  end

end

Change Attributes

class GameTest < MiniTest::Unit::TestCase

  # adding attributes
  #
  # games.yml:
  #
  #   tron:
  #     name:    'Tron'
  #     type:    'Video Game'
  #     rating:  'Gold'
  #
  #
  def test_hash_attributes
    tron = games(:tron)

    tron.change(rating: 'Silver')
    assert_equal 'Silver', tron[:rating]

    tron = tron.mock
    assert_equal 'Silver', tron.rating
  end

end

Create an ActiveRecord Object, but don't save to the database

Create an ActiveRecord Object, and save it to the database

Build Associations

About

The Overlord of Fixtures

License:MIT License


Languages

Language:Ruby 100.0%