Skip to content

Instantly share code, notes, and snippets.

@courtenay
Last active September 18, 2018 03:35
Show Gist options
  • Select an option

  • Save courtenay/7343845 to your computer and use it in GitHub Desktop.

Select an option

Save courtenay/7343845 to your computer and use it in GitHub Desktop.

Revisions

  1. courtenay renamed this gist Nov 6, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. courtenay revised this gist Nov 6, 2013. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,14 @@ class ApiTest < MiniTest::Test
    @test = 5
    end

    def test_fixtures_work
    def test_fixtures
    assert_equal @test, 5
    @test = 6 # let's see if it leaks
    end

    def test_fixtures_again
    assert_equal @test, 5
    @test = 7
    end

    end
  3. courtenay revised this gist Nov 6, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -21,11 +21,16 @@ end
    #
    module TestRunner
    def before_setup
    # todo: start transaction here
    super
    self.class.fixtures.each do |key, val|
    instance_variable_set "#{key}", val
    end
    end
    def before_teardown
    # todo: rollback transaction here
    super
    end
    end

    # Then include it into your test
  4. courtenay revised this gist Nov 6, 2013. No changes.
  5. courtenay created this gist Nov 6, 2013.
    48 changes: 48 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    module Fixtures
    #
    # Set up fixtures that will be defined once during startup
    # and (perhaps) rolled back at the beginning of each test run
    #
    def fixtures &block
    if block_given?
    instance_eval &block
    @fixtures = {}
    (instance_variables - [:@fixtures]).each do |fresh|
    @fixtures[fresh] = instance_variable_get(fresh)
    end
    else
    @fixtures
    end
    end
    end

    # Before each test, look at the class-variables defined
    # and just tragically set them in the instance
    #
    module TestRunner
    def before_setup
    super
    self.class.fixtures.each do |key, val|
    instance_variable_set "#{key}", val
    end
    end
    end

    # Then include it into your test
    class MiniTest::Test
    extend Fixtures
    include TestRunner
    end

    # And define your test
    require 'minitest/autorun'
    class ApiTest < MiniTest::Test

    fixtures do
    @test = 5
    end

    def test_fixtures_work
    assert_equal @test, 5
    end
    end