Skip to content

Instantly share code, notes, and snippets.

@lin
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save lin/f772cde9d9b87b0aac88 to your computer and use it in GitHub Desktop.

Select an option

Save lin/f772cde9d9b87b0aac88 to your computer and use it in GitHub Desktop.
Ruby testing

1, add to gem file

gem "rspec-rails", :group => [:test, :development]
group :test do
  gem "factory_girl_rails"
  gem "capybara"
  gem "guard-rspec"
end

2, run these command

bundle install
rails g rspec:install
mkdir spec/support spec/models spec/routing spec/features
guard init rspec
sudo gem install rb-fsevent

# stubs replace the method, and return a result.
# mocks check if the method get called.
# object stub change a hash to a object instance with methods.
# stubs
test "show_author_summary should set status to zombie summary" do
@tweet.zombie.stubs(:zombie_summary)
@tweet.show_author_summary
assert_equal @tweet.zombie.zombie_summary, @tweet.status, 'tweet status does not contain zombie summary'
end
# mocks
test "status_image calls the ZwitPic get_status_image api" do
ZwitPic.expects(:get_status_image).with(@tweet.id)
.returns(['Scary Zombie', 'http://zwitpic.com/scary_zombie.png'])
@tweet.status_image
end
# object stub
def status_image
image = ZwitPic.get_status_image(self.id)
"<img src='#{image.url}' alt='#{image.name}' />".html_safe
end
# basic test
class ConditionalTest < Test::Unit::TestCase
def test_one_greater_than_zero
assert 1 < 0, "One is not greater than zero"
end
end
# assert
# assert_not_nil
# assert_match
# assert_equal
# assert_raise
class ZombifierTest < Test::Unit::TestCase
def test_brains_in_zombify_raises_error
z = Zombifier.new('BRAINS')
assert_raise(RuntimeError){ z.zombify }
end
end
# assert_kind_of
class ZombifierTest < Test::Unit::TestCase
def test_zombify_returns_a_string
z = Zombifier.new('I like knees')
assert_kind_of String, z.zombify
end
end
# assert_response_to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment