Skip to content

Instantly share code, notes, and snippets.

@moonglum

moonglum/Gemfile Secret

Created September 9, 2014 12:08
Show Gist options
  • Save moonglum/85c935810341a47eca85 to your computer and use it in GitHub Desktop.
Save moonglum/85c935810341a47eca85 to your computer and use it in GitHub Desktop.
Ataru Idea
source "https://rubygems.org"
gem 'minitest'
require 'minitest/autorun'
# This is the setup file that the user provided to you, you just require it.
# The user has to create a module called `Setup` in there.
require_relative 'setup'
# This is a test class that you generate in Ataru. It should work as it does right now,
# with one tiny difference
class MyTest < Minitest::Test
# And this is the difference:
include Setup
# By using this line, all methods defined in the module `Setup` that your user has
# defined will be available in the snippets \o/
# This would be one of the test methods you generate
def test_that_was_created
# And here you can see that we are using one of the methods that the user defined
# in the setup.rb :)
assert_equal let_us_test_this_method, 'hello'
end
end
module Setup
def let_us_test_this_method
return 'hello'
end
end
@moonglum
Copy link
Author

moonglum commented Sep 9, 2014

In the setup.rb file, the user can require the gem that they want to use or other dependencies that they need for testing. Then they can define helper methods that will be available in all the snippets! This could for example be used to make an instance availalbe that you need throughout your test cases! Imagine you have an Addition class that you want to test, and your code examples look like this:

addition.execute 2, 3 #=> 5

Then Ruby would be sad, because addition is not defined. Not any more when you have the following setup.rb:

require 'addition'

module Setup
  def addition
    Addition.new
  end
end

Now addition will return a fresh instance for you, so it can be used in your tests 👍 And it could also take all kinds of arguments in its constructor if necessary, because it is pure Ruby!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment