From f372cec4fd876eeed2326810e7c6d48fcc1cd0f4 Mon Sep 17 00:00:00 2001 From: Frank O'Hara Date: Sat, 18 May 2013 13:11:30 -0500 Subject: [PATCH] Replace Test::Unit with RSpec Existing tests have been rewritten using RSpec. --- Gemfile | 2 +- Rakefile | 10 ++-- red-glass.gemspec | 1 + spec/red_glass_app_spec.rb | 55 +++++++++++++++++++ .../red_glass_spec.rb | 14 ++--- spec/spec_helper.rb | 8 +++ test/test_red_glass_app.rb | 50 ----------------- 7 files changed, 74 insertions(+), 66 deletions(-) create mode 100644 spec/red_glass_app_spec.rb rename test/test_red_glass.rb => spec/red_glass_spec.rb (56%) create mode 100644 spec/spec_helper.rb delete mode 100644 test/test_red_glass_app.rb diff --git a/Gemfile b/Gemfile index f6a560f..cd8aa9e 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ -source :rubygems +source 'https://rubygems.org' gemspec \ No newline at end of file diff --git a/Rakefile b/Rakefile index 336ff01..3c4d7c1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,6 @@ -require 'rake/testtask' +require 'rspec/core/rake_task' -Rake::TestTask.new do |t| - t.test_files = Dir.glob("test/**/test_*.rb") -end +RSpec::Core::RakeTask.new(:spec) -desc "Run tests" -task :default => :test \ No newline at end of file +desc 'Run specs' +task :default => :spec \ No newline at end of file diff --git a/red-glass.gemspec b/red-glass.gemspec index 2795a9b..f14687f 100644 --- a/red-glass.gemspec +++ b/red-glass.gemspec @@ -15,4 +15,5 @@ Gem::Specification.new do |s| s.add_dependency 'json' s.add_dependency 'uuid' s.add_development_dependency 'rack-test' + s.add_development_dependency 'rspec' end diff --git a/spec/red_glass_app_spec.rb b/spec/red_glass_app_spec.rb new file mode 100644 index 0000000..e4f0e34 --- /dev/null +++ b/spec/red_glass_app_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' +require_relative '../lib/red-glass/red-glass-app' + +describe 'RedGlass App' do + + def app + RedGlassApp + end + + let(:event_json) { {"id"=>"", "url"=>"/", "testID"=>"ef100740-4860-012f-0d6d-00254bac7e96", "time"=>1330890507501, + "type"=>"click", "pageX"=>592, "pageY"=>516, + "target"=>"html > body > div#main > div#inner.gainlayout > div.form_controls > a"}} + let(:event_keys) { %w(url testID time type target) } + + describe 'status' do + describe 'GET' do + it 'returns a ready message' do + get '/status' + last_response.body.should eq 'ready' + end + end + end + + describe 'events' do + describe 'GET' do + context 'when no events are available' do + it 'returns an empty array' do + get '/events' + last_response.body.should eq '[]' + end + end + end + + describe 'POST' do + it 'returns multiple events' do + 2.times do + post '/', 'event_json' => event_json.to_json + end + get '/events' + JSON.parse(last_response.body).size.should eq 2 + end + it 'returns a success response code' do + post '/', 'event_json' => event_json.to_json + last_response.ok?.should be_true + end + it 'returns correctly structured JSON' do + post '/', 'event_json' => event_json.to_json + get '/events' + event_keys.each do |key| + JSON.parse(last_response.body).first[key].should eq event_json[key] + end + end + end + end +end \ No newline at end of file diff --git a/test/test_red_glass.rb b/spec/red_glass_spec.rb similarity index 56% rename from test/test_red_glass.rb rename to spec/red_glass_spec.rb index add0fe1..03055d2 100644 --- a/test/test_red_glass.rb +++ b/spec/red_glass_spec.rb @@ -1,12 +1,9 @@ +require 'spec_helper' require_relative '../lib/red-glass/red_glass' -require "test/unit" -require "selenium-webdriver" -require "json" -class TestRedGlass < Test::Unit::TestCase - PROJ_ROOT = File.dirname(__FILE__).to_s +describe 'RedGlass' do - def test_events_captured_in_browser_and_sent_to_red_glass_app_server + it 'captures browser events and sends them to the RedGlass app server' do driver = Selenium::WebDriver.for :firefox red_glass = RedGlass.new driver @@ -17,10 +14,9 @@ def test_events_captured_in_browser_and_sent_to_red_glass_app_server driver.quit uri = URI.parse("http://localhost:4567/events") event = JSON.parse(Net::HTTP.get_response(uri).body.to_s)[0] - ['url', 'testID', 'time', 'type', 'target'].each do |property| - assert !event[property].nil? + %w(url testID time type target).each do |property| + event[property].nil?.should be_false end red_glass.stop end - end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..2205556 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,8 @@ +require 'rspec' +require 'selenium-webdriver' +require 'json' +require 'rack/test' + +RSpec.configure do |conf| + conf.include Rack::Test::Methods +end \ No newline at end of file diff --git a/test/test_red_glass_app.rb b/test/test_red_glass_app.rb deleted file mode 100644 index 36929f9..0000000 --- a/test/test_red_glass_app.rb +++ /dev/null @@ -1,50 +0,0 @@ -require_relative '../lib/red-glass/red-glass-app' -require 'test/unit' -require 'rack/test' -require 'json' - -class TestRedGlassApp < Test::Unit::TestCase - include Rack::Test::Methods - - def setup - @event_json = {"id"=>"", "url"=>"/", "testID"=>"ef100740-4860-012f-0d6d-00254bac7e96", "time"=>1330890507501, "type"=>"click", "pageX"=>592, "pageY"=>516, "target"=>"html > body > div#main > div#inner.gainlayout > div.form_controls > a"} - end - - def app - RedGlassApp.new - end - - def test_status - get '/status' - assert_equal 'ready', last_response.body - end - - def test_post_event - post '/', "event_json" => @event_json.to_json - assert last_response.ok? - end - - def test_get_empty_events - get '/events' - assert_equal '[]', last_response.body - end - - def test_get_single_event - post '/', "event_json" => @event_json.to_json - get '/events' - assert_equal @event_json['url'], JSON.parse(last_response.body)[0]['url'] - assert_equal @event_json['testID'], JSON.parse(last_response.body)[0]['testID'] - assert_equal @event_json['time'], JSON.parse(last_response.body)[0]['time'] - assert_equal @event_json['type'], JSON.parse(last_response.body)[0]['type'] - assert_equal @event_json['target'], JSON.parse(last_response.body)[0]['target'] - end - - def test_get_multiple_events - 2.times do - post '/', "event_json" => @event_json.to_json - end - get '/events' - assert_equal 2, JSON.parse(last_response.body).size - end - -end \ No newline at end of file