Skip to content

Instantly share code, notes, and snippets.

@JacobNinja
Created December 3, 2011 00:36
Show Gist options
  • Save JacobNinja/1425526 to your computer and use it in GitHub Desktop.
Save JacobNinja/1425526 to your computer and use it in GitHub Desktop.
Test stubs
# Creates test stubs for logic in methods
require 'rspec'
require File.expand_path('../../test_stubs.rb', __FILE__)
describe TestStubs do
let(:sut) { TestStubs.new(@code) }
describe "#generate" do
it "creates test suite for empty class" do
@code = "class Test; end"
sut.generate.should eql(<<-TESTSUITE.chomp)
class TestTests < Test::Unit::TestCase
end
TESTSUITE
end
it "creates test suite and test for class with empty method" do
@code = "class Test; def method_name; end; end"
sut.generate.should eql(<<-TESTSUITE.chomp)
class TestTests < Test::Unit::TestCase
def test_method_name
end
end
TESTSUITE
end
it "creates test suite and tests with conditional" do
@code = "class Test; def method_name; return value if nil; end; end"
sut.generate.should eql(<<-TESTSUITE.chomp)
class TestTests < Test::Unit::TestCase
def test_method_name_returns_value_if_nil
end
end
TESTSUITE
end
it "creates test suite and tests with if and else" do
@code = "class Test; def method_name; if condition; 5; else; 10; end; end; end"
sut.generate.should eql(<<-TESTSUITE.chomp)
class TestTests < Test::Unit::TestCase
def test_method_name_returns_5_if_condition
end
def test_method_name_returns_10_if_not_condition
end
end
TESTSUITE
end
it "creates test suite and test with nested conditional" do
@code = <<-CODE
class Test
def method_name
if condition
if condition2
1
else
2
end
else
3
end
end
end
CODE
sut.generate.should eql(<<-TESTSUITE.chomp)
class TestTests < Test::Unit::TestCase
def test_method_name_returns_1_if_condition_and_condition2
end
def test_method_name_returns_2_if_condition_and_not_condition2
end
def test_method_name_returns_3_if_not_condition
end
end
TESTSUITE
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment