Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adopt the RSpec reporter protocol

  • Loading branch information...
commit aa11e4d76f4384bb28a3b69f2e80253314ad92cf 1 parent b82299b
@ragalie ragalie authored
View
1  History.md
@@ -5,6 +5,7 @@
* Removed support for konacha_config.js and Konacha.mochaOptions in favor of
Mocha's own configuration methods. See the README for update instructions.
* Update mocha (1.6.0) and chai (1.3.0)
+* Adopt the RSpec reporter interface
# 1.x-stable
View
25 LICENSE
@@ -88,3 +88,28 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+
+Portions derived from RSpec (https://github.com/rspec/rspec-core)
+
+Copyright (c) 2009 Chad Humphries, David Chelimsky
+Copyright (c) 2006 David Chelimsky, The RSpec Development Team
+Copyright (c) 2005 Steven Baker
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
16 README.md
@@ -112,6 +112,22 @@ $ bundle exec rake konacha:run SPEC=foo_spec
$ bundle exec rake konacha:run SPEC=foo_spec,bar_spec,etc_spec
```
+Konacha includes a default formatter modeled upon RSpec's ProgressFormatter.
+Additionally, Konacha's runner implements the same protocol as RSpec, so many
+RSpec formatters also work with Konacha.
+
+To specify one or more formatters, provide a comma separated list of class names
+to the `FORMAT` environment variable. Output can be directed to a file by
+appending a colon and the file path to the class name.
+
+```
+$ bundle exec rake konacha:run FORMAT=Konacha::Formatter,RSpec::Core::Formatters::ProgressFormatter
+$ bundle exec rake konacha:run FORMAT=Konacha::Formatter:results.txt
+```
+
+You will need to `require` any formatters you use. It's a good idea to do this
+within a `defined?` check in your [Konacha initializer](#configuration).
+
To automatically trigger reruns when files change, try [guard-konacha](https://github.com/alexgb/guard-konacha).
## Spec Helper
View
71 app/assets/javascripts/konacha/runner.js
@@ -1,45 +1,70 @@
Konacha = {
- dots: "",
- getResults: function() {
- return JSON.stringify(Konacha.results);
+ getEvents: function() {
+ return JSON.stringify(Konacha.events);
}
};
mocha.reporter(function(runner) {
+ var createTestObject = function(test, status) {
+ var obj = {
+ title:test.title,
+ fullTitle:test.fullTitle(),
+ duration:test.duration,
+ parentFullTitle:test.parent.fullTitle(),
+ status:status
+ };
+
+ if (status == "failed")
+ obj.error = test.err; // Contains message, expected, actual, operator, stack
+
+ return obj;
+ };
+
+ var createSuiteObject = function(suite) {
+ var obj = {
+ title:suite.title,
+ fullTitle:suite.fullTitle()
+ };
+
+ if (suite.parent)
+ obj.parentFullTitle = suite.parent.fullTitle();
+
+ return obj;
+ };
+
Mocha.reporters.Base.call(this, runner);
runner.on('start', function() {
- Konacha.results = [];
+ Konacha.events = [];
+ });
+
+ runner.on('suite', function(suite) {
+ if (suite.fullTitle() && suite.fullTitle().length > 0)
+ Konacha.events.push({event:'suite', data:createSuiteObject(suite), type:'suite'});
+ });
+
+ runner.on('test', function(test) {
+ Konacha.events.push({event:'test', data:createTestObject(test), type:'test'});
});
runner.on('pass', function(test) {
- Konacha.dots += ".";
- Konacha.results.push({
- name:test.title,
- passed:true
- });
+ Konacha.events.push({event:'pass', data:createTestObject(test, "passed"), type:'test'});
});
runner.on('fail', function(test) {
- Konacha.dots += "F";
- Konacha.results.push({
- name:test.title,
- passed:false,
- message:test.err.message,
- trace:test.err.stack
- });
+ Konacha.events.push({event:'fail', data:createTestObject(test, "failed"), type:'test'});
});
runner.on('pending', function(test) {
- Konacha.dots += "P";
- Konacha.results.push({
- name:test.title,
- passed:false,
- pending:true
- });
+ Konacha.events.push({event:'pending', data:createTestObject(test, "pending"), type:'test'});
+ });
+
+ runner.on('suite end', function(suite) {
+ if (suite.fullTitle() && suite.fullTitle().length > 0)
+ Konacha.events.push({event:'suite end', data:createSuiteObject(suite), type:'suite'});
});
runner.on('end', function() {
- Konacha.done = true;
+ Konacha.events.push({event:'end', data:{}});
});
});
View
2  lib/konacha.rb
@@ -2,6 +2,8 @@
require "konacha/engine"
require "konacha/runner"
require "konacha/server"
+require "konacha/reporter"
+require "konacha/formatter"
module Konacha
class << self
View
1  lib/konacha/engine.rb
@@ -32,6 +32,7 @@ def self.application(app)
options.application ||= self.class.application(app)
options.driver ||= :selenium
options.stylesheets ||= %w(application)
+ options.verbose ||= false
app.config.assets.paths << app.root.join(options.spec_dir).to_s
end
View
80 lib/konacha/formatter.rb
@@ -0,0 +1,80 @@
+require "colorize"
+
+module Konacha
+ class Formatter
+ attr_reader :io, :examples
+
+ def initialize(io)
+ @io = io
+ @examples = []
+ end
+
+ def start(expected_example_count=nil); end
+ def example_group_started(group); end
+ def example_started(example); end
+
+ def example_passed(example)
+ @examples << example
+ io.write(".".green)
+ end
+
+ def example_failed(example)
+ @examples << example
+ io.write("F".red)
+ end
+
+ def example_pending(example)
+ @examples << example
+ io.write("P".yellow)
+ end
+
+ def example_group_finished(group); end
+ def stop; end
+
+ def start_dump
+ io.puts ""
+ end
+
+ def dump_pending
+ pending_examples = examples.select(&:pending?)
+ if pending_examples.present?
+ io.puts ""
+ io.puts(pending_examples.map {|example| pending_message(example)}.join("\n\n"))
+ end
+ end
+
+ def dump_failures
+ failed_examples = examples.select(&:failed?)
+ if failed_examples.present?
+ io.puts ""
+ io.puts(failed_examples.map {|example| failure_message(example)}.join("\n\n"))
+ end
+ end
+
+ def dump_summary(duration, example_count, failure_count, pending_count)
+ seconds = "%.2f" % duration
+ io.puts ""
+ io.puts "Finished in #{seconds} seconds"
+ io.puts "#{example_count} examples, #{failure_count} failed, #{pending_count} pending"
+ end
+
+ def seed(seed); end
+
+ def close
+ io.close
+ end
+
+ private
+ def failure_message(example)
+ msg = []
+ msg << " Failed: #{example.full_description}"
+ msg << " #{example.exception.message}"
+ msg << " in #{example.exception.backtrace.first}" if example.exception.backtrace.present?
+ msg.join("\n").red
+ end
+
+ def pending_message(example)
+ " Pending: #{example.full_description}".yellow
+ end
+ end
+end
View
91 lib/konacha/reporter.rb
@@ -0,0 +1,91 @@
+require "konacha/reporter/example"
+require "konacha/reporter/example_group"
+
+# The Konacha Reporter implements the same protocol as the RSpec Reporter.
+# More details on the RSpec Reporter protocol are available in the rspec-core
+# repository: https://github.com/rspec/rspec-core/blob/1852a7f4221c5731e108484cb2debfbaca60b283/lib/rspec/core/formatters/base_formatter.rb#L8-L25
+
+module Konacha
+ class Reporter
+ EVENT_CONVERSIONS = {
+ 'suite' => :example_group_started,
+ 'test' => :example_started,
+ 'pass' => :example_passed,
+ 'fail' => :example_failed,
+ 'pending' => :example_pending,
+ 'suite end' => :example_group_finished,
+ }
+
+ attr_reader :start_time, :duration, :example_count, :failure_count, :pending_count
+
+ def initialize(*formatters)
+ @formatters = formatters
+ @example_count = @failure_count = @pending_count = 0
+ @duration = @start_time = nil
+ @examples, @groups = {}, {}
+ end
+
+ def start(expected_example_count=nil)
+ @start_time = Time.now
+ process_event :start, expected_example_count
+ end
+
+ def finish(seed=nil)
+ begin
+ stop
+ process_event :start_dump
+ process_event :dump_pending
+ process_event :dump_failures
+ process_event :dump_summary, @duration, @example_count, @failure_count, @pending_count
+ process_event :seed, seed if seed
+ ensure
+ process_event :close
+ end
+ end
+
+ def stop
+ @duration = Time.now - @start_time if @start_time
+ process_event :stop
+ end
+
+ def passed?
+ @examples.values.all? { |example| example.passed? || example.pending? }
+ end
+
+ def process_mocha_event(event)
+ if event['type']
+ object = update_or_create_object(event['data'], event['type'])
+ process_event EVENT_CONVERSIONS[event['event']], object
+ end
+ end
+
+ def process_event(method, *args, &block)
+ case method
+ when :example_started
+ @example_count += 1
+ when :example_failed
+ @failure_count += 1
+ when :example_pending
+ @pending_count += 1
+ end
+
+ @formatters.each do |formatter|
+ formatter.send method, *args, &block
+ end
+ end
+
+ def update_or_create_object(data, type)
+ collection = type == 'test' ? @examples : @groups
+ object = collection[data['fullTitle']]
+ if object
+ object.update_metadata(data)
+ else
+ klass = type == 'test' ? Example : ExampleGroup
+ parent = @groups[data['parentFullTitle']]
+ object = collection[data['fullTitle']] = klass.new(data, parent)
+ end
+
+ object
+ end
+ end
+end
View
34 lib/konacha/reporter/example.rb
@@ -0,0 +1,34 @@
+require "konacha/reporter/metadata"
+
+# The Example class mimics the public interface of RSpec::Core::Example.
+
+module Konacha
+ class Reporter
+ class Example
+ attr_reader :metadata, :parent
+
+ def initialize(data, parent)
+ @metadata = Metadata.new(data)
+ @parent = parent
+ end
+
+ delegate :full_description, :description, :location, :file_path, :line_number, :pending, :pending_message, :exception, :execution_result, :to => :metadata
+
+ alias_method :pending?, :pending
+ alias_method :options, :metadata
+ alias_method :example_group, :parent
+
+ def passed?
+ execution_result[:status] == "passed"
+ end
+
+ def failed?
+ execution_result[:status] == "failed"
+ end
+
+ def update_metadata(data)
+ metadata.update(data)
+ end
+ end
+ end
+end
View
37 lib/konacha/reporter/example_group.rb
@@ -0,0 +1,37 @@
+require "konacha/reporter/metadata"
+
+# The ExampleGroup class mimics the public interface of RSpec::Core::ExampleGroup.
+
+module Konacha
+ class Reporter
+ class ExampleGroup
+ attr_reader :metadata, :parent
+
+ def initialize(data, parent)
+ @metadata = Metadata.new(data)
+ @parent = parent
+ end
+
+ delegate :full_description, :description, :file_path, :described_class, :to => :metadata
+
+ alias_method :display_name, :description
+
+ def parent_groups
+ ancestor = parent
+ groups = []
+ while ancestor
+ groups << ancestor
+ ancestor = ancestor.parent
+ end
+
+ groups
+ end
+
+ alias_method :ancestors, :parent_groups
+
+ def update_metadata(data)
+ metadata.update(data)
+ end
+ end
+ end
+end
View
81 lib/konacha/reporter/metadata.rb
@@ -0,0 +1,81 @@
+# The Metadata class mimics the public interface of RSpec::Core::Metadata.
+
+module Konacha
+ class Reporter
+ class SpecException < Exception
+ def pending_fixed?
+ false
+ end
+ end
+
+ class Metadata
+ attr_reader :data
+
+ def initialize(data)
+ @data = data
+ end
+
+ def [](key)
+ respond_to?(key) ? send(key) : data[key]
+ end
+
+ def update(data)
+ @data.merge!(data)
+ end
+
+ def file_path
+ STDERR.puts "file_path not implemented" if Konacha.config.verbose
+ "" # RSpec's BaseFormatter expects the return value to be a string
+ end
+
+ alias_method :location, :file_path
+
+ def line_number
+ STDERR.puts "line_number not implemented" if Konacha.config.verbose
+ nil
+ end
+
+ def execution_result
+ @execution_result ||= {
+ :status => data['status'],
+ :started_at => nil,
+ :finished_at => nil,
+ :run_time => data['duration'],
+ :exception => exception
+ }
+ end
+
+ def exception
+ return unless data['status'] == "failed"
+
+ @exception ||= begin
+ e = Reporter::SpecException.new(data['error']['message'])
+ e.set_backtrace([])
+ e
+ end
+ end
+
+ def pending_message
+ STDERR.puts "pending_message not implemented" if Konacha.config.verbose
+ nil
+ end
+
+ def described_class
+ STDERR.puts "described_class not implemented" if Konacha.config.verbose
+ nil
+ end
+
+ def pending
+ data['status'] == "pending"
+ end
+
+ def description
+ data['title']
+ end
+
+ def full_description
+ data['fullTitle']
+ end
+ end
+ end
+end
View
104 lib/konacha/runner.rb
@@ -1,5 +1,4 @@
require "capybara"
-require "colorize"
module Konacha
class Runner
@@ -7,105 +6,54 @@ def self.start
new.run
end
- attr_reader :io, :examples
+ attr_reader :reporter
- def initialize(options = {})
- @io = options[:output] || STDOUT
+ def initialize
+ @reporter = Konacha::Reporter.new(*formatters)
end
def run
- before = Time.now
+ reporter.start
begin
- session.visit("/")
+ session.visit('/')
- dots_printed = 0
+ events_consumed = 0
+ done = false
begin
sleep 0.1
- done, dots = session.evaluate_script('[Konacha.done, Konacha.dots]')
- if dots
- io.write colorize_dots(dots[dots_printed..-1])
- io.flush
- dots_printed = dots.length
+ events = JSON.parse(session.evaluate_script('Konacha.getEvents()'))
+ if events
+ events[events_consumed..-1].each do |event|
+ done = true if event['event'] == 'end'
+ reporter.process_mocha_event(event)
+ end
+
+ events_consumed = events.length
end
end until done
-
- @examples = JSON.parse(session.evaluate_script('Konacha.getResults()')).map do |row|
- Example.new(row)
- end
rescue => e
raise e, "Error communicating with browser process: #{e}", e.backtrace
end
- io.puts ""
- io.puts ""
- failure_messages.each { |msg| io.write("#{msg}\n\n") }
-
- seconds = "%.2f" % (Time.now - before)
- io.puts "Finished in #{seconds} seconds"
- io.puts "#{examples.size} examples, #{failed_examples.size} failures, #{pending_examples.size} pending"
- passed?
- end
-
- def pending_examples
- examples.select { |example| example.pending? }
- end
-
- def failed_examples
- examples.select { |example| example.failed? }
- end
-
- def passed?
- examples.all? { |example| example.passed? || example.pending? }
- end
-
- def failure_messages
- examples.map { |example| example.failure_message }.compact
+ reporter.finish
+ reporter.passed?
end
def session
@session ||= Capybara::Session.new(Konacha.driver, Konacha.application)
end
- def colorize_dots(dots)
- dots = dots.chars.map do |d|
- case d
- when 'E', 'F'; d.red
- when 'P'; d.yellow
- when '.'; d.green
- else; d
+ private
+ def formatters
+ if ENV['FORMAT']
+ ENV['FORMAT'].split(',').map do |string|
+ string.match(/^(.*?[^:]+):?([^:]+)?$/)
+ klass, path = $1, $2
+ eval(klass).new(path ? File.open(path, 'w') : STDOUT)
end
- end
- dots.join ''
- end
- end
-
- class Example
- def initialize(row)
- @row = row
- end
-
- def passed?
- @row['passed']
- end
-
- def pending?
- @row['pending']
- end
-
- def failed?
- !(@row['passed'] || @row['pending'])
- end
-
- def failure_message
- if failed?
- msg = []
- msg << " Failed: #{@row['name']}"
- msg << " #{@row['message']}"
- msg << " in #{@row['trace']['fileName']}:#{@row['trace']['lineNumber']}" if @row['trace']
- msg.join("\n").red
- elsif pending?
- " Pending: #{@row['name']}".yellow
+ else
+ [Konacha::Formatter.new(STDOUT)]
end
end
end
View
69 spec/formatter_spec.rb
@@ -0,0 +1,69 @@
+require 'spec_helper'
+
+describe Konacha::Formatter do
+ let(:io) { StringIO.new }
+ subject { described_class.new(io) }
+
+ shared_examples "test result" do |method, dot|
+ it "stores the example in the examples array" do
+ subject.send(method, nil)
+ subject.examples.should be_present
+ end
+
+ it "outputs the dot" do
+ subject.send(method, nil)
+ io.rewind
+ io.read.should include(dot)
+ end
+ end
+
+ describe "#example_passed" do
+ it_behaves_like "test result", :example_passed, "."
+ end
+
+ describe "#example_failed" do
+ it_behaves_like "test result", :example_failed, "F"
+ end
+
+ describe "#example_pending" do
+ it_behaves_like "test result", :example_pending, "P"
+ end
+
+ describe "#dump_pending" do
+ let(:example) { double('example', :pending? => true, :full_description => "Pending example") }
+ before { subject.stub(:examples => [example]) }
+
+ it "outputs the pending message" do
+ subject.dump_pending
+ io.rewind
+ io.read.should include(" Pending: Pending example")
+ end
+ end
+
+ describe "#dump_failures" do
+ let(:example) do
+ double('example',
+ :failed? => true,
+ :full_description => "Failed example",
+ :exception => double('exception',
+ :message => "exception",
+ :backtrace => nil))
+ end
+
+ before { subject.stub(:examples => [example]) }
+
+ it "outputs the failure message" do
+ subject.dump_failures
+ io.rewind
+ io.read.should include(" Failed: Failed example\n exception")
+ end
+ end
+
+ describe "#dump_summary" do
+ it "outputs the summary" do
+ subject.dump_summary(10, 10, 2, 3)
+ io.rewind
+ io.read.should == "\nFinished in 10.00 seconds\n10 examples, 2 failed, 3 pending\n"
+ end
+ end
+end
View
64 spec/reporter/example_group_spec.rb
@@ -0,0 +1,64 @@
+require 'spec_helper'
+
+describe Konacha::Reporter::ExampleGroup do
+ subject { described_class.new({}, nil) }
+
+ describe "#initialize" do
+ it "loads up a metadata instance and the parent" do
+ data = double('data')
+ parent = double('parent')
+ example_group = described_class.new(data, parent)
+
+ example_group.parent.should == parent
+ example_group.metadata.should be_a(Konacha::Reporter::Metadata)
+ example_group.metadata.data.should == data
+ end
+ end
+
+ describe "delegated methods" do
+ let(:metadata) { double('metadata') }
+ before { Konacha::Reporter::Metadata.stub(:new) { metadata } }
+
+ [:full_description, :description, :file_path, :described_class].each do |method|
+ it "delegates #{method} to metadata" do
+ metadata.should_receive(method)
+ subject.send(method)
+ end
+ end
+ end
+
+ describe "aliased_methods" do
+ it "aliases display_name to description" do
+ subject.display_name.should == subject.description
+ end
+ end
+
+ describe "#parent_groups" do
+ let(:parent) { double('parent') }
+ let(:grandparent) { double('grandparent') }
+
+ before do
+ grandparent.stub(:parent => nil)
+ parent.stub(:parent => grandparent)
+ subject.stub(:parent => parent)
+ end
+
+ it "finds all of this group's ancestors" do
+ subject.parent_groups.should == [parent, grandparent]
+ end
+
+ it "works via #ancestors" do
+ subject.ancestors.should == [parent, grandparent]
+ end
+ end
+
+ describe "#update_metadata" do
+ it "calls metadata.update" do
+ data = double('data')
+ metadata = double('metadata')
+ metadata.should_receive(:update).with(data)
+ Konacha::Reporter::Metadata.stub(:new) { metadata }
+ subject.update_metadata(data)
+ end
+ end
+end
View
77 spec/reporter/example_spec.rb
@@ -0,0 +1,77 @@
+require 'spec_helper'
+
+describe Konacha::Reporter::Example do
+ subject { described_class.new({}, nil) }
+
+ describe "#initialize" do
+ it "loads up a metadata instance and the parent" do
+ data = double('data')
+ parent = double('parent')
+ example = described_class.new(data, parent)
+
+ example.parent.should == parent
+ example.metadata.should be_a(Konacha::Reporter::Metadata)
+ example.metadata.data.should == data
+ end
+ end
+
+ describe "delegated methods" do
+ let(:metadata) { double('metadata') }
+ before { Konacha::Reporter::Metadata.stub(:new) { metadata } }
+
+ [:full_description, :description, :location, :file_path, :line_number, :pending, :pending_message, :exception, :execution_result].each do |method|
+ it "delegates #{method} to metadata" do
+ metadata.should_receive(method)
+ subject.send(method)
+ end
+ end
+ end
+
+ describe "aliased_methods" do
+ it "aliases pending? to pending" do
+ subject.pending?.should == subject.pending
+ end
+
+ it "aliases options to metadata" do
+ subject.options.should == subject.metadata
+ end
+
+ it "aliases example_group to parent" do
+ subject.example_group.should == subject.parent
+ end
+ end
+
+ describe "#passed?" do
+ it "returns true iff execution_result[:status] is passed" do
+ subject.should_receive(:execution_result) { {:status => "passed"} }
+ subject.passed?.should be_true
+ end
+
+ it "returns false" do
+ subject.should_receive(:execution_result) { {} }
+ subject.passed?.should be_false
+ end
+ end
+
+ describe "#failed?" do
+ it "returns true iff execution_result[:status] is failed" do
+ subject.should_receive(:execution_result) { {:status => "failed"} }
+ subject.failed?.should be_true
+ end
+
+ it "returns false" do
+ subject.should_receive(:execution_result) { {} }
+ subject.failed?.should be_false
+ end
+ end
+
+ describe "#update_metadata" do
+ it "calls metadata.update" do
+ data = double('data')
+ metadata = double('metadata')
+ metadata.should_receive(:update).with(data)
+ Konacha::Reporter::Metadata.stub(:new) { metadata }
+ subject.update_metadata(data)
+ end
+ end
+end
View
68 spec/reporter/metadata_spec.rb
@@ -0,0 +1,68 @@
+require 'spec_helper'
+
+describe Konacha::Reporter::Metadata do
+ subject { described_class.new({}) }
+
+ describe "#[]" do
+ it "calls the method on self if available" do
+ subject.should_receive(:full_description)
+ subject[:full_description]
+ end
+
+ it "looks in the data hash" do
+ subject.should_receive(:data) { {} }
+ subject[:title]
+ end
+ end
+
+ describe "#update" do
+ it "merges the new data into the data hash" do
+ subject.update(:title => "test")
+ subject.data.should == {:title => "test"}
+ end
+ end
+
+ describe "#exception" do
+ it "returns unless the spec failed" do
+ subject.exception.should be_nil
+ end
+
+ it "builds a SpecException object" do
+ subject.update('status' => 'failed', 'error' => {'message' => 'expected this to work'})
+ subject.exception.should be_a(Konacha::Reporter::SpecException)
+ subject.exception.message.should == 'expected this to work'
+ subject.exception.backtrace.should == []
+ end
+ end
+
+ describe "#execution_result" do
+ it "returns a hash with execution details" do
+ subject.execution_result.keys.sort.should == [:exception, :finished_at, :run_time, :started_at, :status]
+ end
+ end
+
+ describe "#pending" do
+ it "returns true iff status is pending" do
+ subject.update('status' => 'pending')
+ subject.pending.should be_true
+ end
+
+ it "returns false" do
+ subject.pending.should be_false
+ end
+ end
+
+ describe "#description" do
+ it "looks for data['title']" do
+ subject.update('title' => 'super test')
+ subject.description.should == 'super test'
+ end
+ end
+
+ describe "#full_description" do
+ it "looks for data['fullTitle']" do
+ subject.update('fullTitle' => 'super test')
+ subject.full_description.should == 'super test'
+ end
+ end
+end
View
112 spec/reporter_spec.rb
@@ -0,0 +1,112 @@
+require 'spec_helper'
+
+describe Konacha::Reporter do
+ let(:formatter) { double('formatter').as_null_object }
+ subject { described_class.new(formatter) }
+
+ describe "#start" do
+ it "sets the start time to the current time" do
+ subject.start
+ subject.start_time.should be_within(1.second).of(Time.now)
+ end
+
+ it "processes the start event" do
+ subject.should_receive(:process_event).with(:start, nil)
+ subject.start
+ end
+ end
+
+ describe "#finish" do
+ it "calls #stop" do
+ subject.should_receive(:stop)
+ subject.finish
+ end
+
+ it "processes events" do
+ subject.stub(:stop)
+ subject.should_receive(:process_event).with(:start_dump).ordered
+ subject.should_receive(:process_event).with(:dump_pending).ordered
+ subject.should_receive(:process_event).with(:dump_failures).ordered
+ subject.should_receive(:process_event).with(:dump_summary, nil, 0, 0, 0).ordered
+ subject.should_receive(:process_event).with(:close).ordered
+ subject.finish
+ end
+ end
+
+ describe "#stop" do
+ it "calculates duration" do
+ subject.start
+ subject.stop
+ subject.duration.should_not be_nil
+ end
+
+ it "processes the stop event" do
+ subject.should_receive(:process_event).with(:stop)
+ subject.stop
+ end
+ end
+
+ describe "#process_mocha_event" do
+ it "creates the object" do
+ subject.stub(:process_event)
+ subject.should_receive(:update_or_create_object).with('data', 'type')
+ subject.process_mocha_event({'data' => 'data', 'type' => 'type'})
+ end
+
+ it "calls #process_event with the converted event name" do
+ object = double('test')
+ subject.stub(:update_or_create_object) { object }
+ subject.should_receive(:process_event).with(:example_started, object)
+ subject.process_mocha_event({'event' => 'test', 'type' => 'test'})
+ end
+ end
+
+ describe "#process_event" do
+ describe "increments counts" do
+ it "increments example count" do
+ subject.process_event(:example_started)
+ subject.example_count.should == 1
+ end
+
+ it "increments pending count" do
+ subject.process_event(:example_pending)
+ subject.pending_count.should == 1
+ end
+
+ it "increments failed count" do
+ subject.process_event(:example_failed)
+ subject.failure_count.should == 1
+ end
+ end
+
+ it "forwards the call on to the formatters" do
+ formatter.should_receive(:example_started).with('arg!')
+ subject.process_event(:example_started, 'arg!')
+ end
+ end
+
+ describe "#update_or_create_object" do
+ describe "creates the right type of object" do
+ it "creates example if test" do
+ subject.update_or_create_object({}, 'test').should be_a(Konacha::Reporter::Example)
+ end
+
+ it "creates example_group if suite" do
+ subject.update_or_create_object({}, 'suite').should be_a(Konacha::Reporter::ExampleGroup)
+ end
+ end
+
+ it "updates if the object with same fullTitle exists" do
+ data = {'fullTitle' => 'title'}
+ object = subject.update_or_create_object(data, 'test')
+ object.should_receive(:update_metadata).with(data)
+ subject.update_or_create_object(data, 'test').should == object
+ end
+
+ it "links up the parent correctly" do
+ suite = subject.update_or_create_object({'fullTitle' => 'suite'}, 'suite')
+ object = subject.update_or_create_object({'fullTitle' => 'suite awesome', 'parentFullTitle' => 'suite'}, 'test')
+ object.parent.should == suite
+ end
+ end
+end
View
88 spec/runner_spec.rb
@@ -4,28 +4,78 @@
before do
Konacha.mode = :runner
Konacha.config.driver = :selenium_with_firebug
+ STDOUT.stub(:puts)
end
- let(:runner) { Konacha::Runner.new(:output => buffer) }
- let(:buffer) { StringIO.new }
-
describe "#run" do
- before { runner.run }
-
- it "prints results to the output" do
- buffer.rewind
- results = buffer.read
- # Strip colors
- results.gsub!(/\e\[([0-9]{1,2}(;[0-9]{1,2})*)?[m|K]/, '')
- # Failure output present?
- results.should include 'F'
- results.should include 'expected 4 to equal 5'
- # Pending output present?
- results.should include 'P'
- results.should include 'is pending'
- # Summary and dots
- results.should include "#{runner.examples.length} examples, 1 failures, 1 pending"
- results.should match /^[.FP]{#{runner.examples.length}}$/
+ let(:suite) do
+ {'event' => 'suite',
+ 'type' => 'suite',
+ 'data' => {
+ 'title' => 'failure',
+ 'fullTitle' => 'failure'
+ }}
+ end
+
+ let(:suite_end) do
+ {'event' => 'suite end',
+ 'type' => 'suite',
+ 'data' => {
+ 'title' => 'failure',
+ 'fullTitle' => 'failure'
+ }}
+ end
+
+ let(:test) do
+ {'event' => 'test',
+ 'type' => 'test',
+ 'data' => {
+ 'title' => 'fails',
+ 'fullTitle' => 'failure fails',
+ 'parentFullTitle' => 'failure'}}
+ end
+
+ let(:failure) do
+ {'event' => 'fail',
+ 'type' => 'test',
+ 'data' => {
+ 'title' => 'fails',
+ 'fullTitle' => 'failure fails',
+ 'parentFullTitle' => 'failure',
+ 'status' => 'failed',
+ 'error' => {'message' => 'expected 4 to equal 5', 'expected' => 5}}}
+ end
+
+ let(:pass) do
+ {'event' => 'pass',
+ 'type' => 'test',
+ 'data' => {
+ 'title' => 'is empty',
+ 'fullTitle' => 'the body#konacha element is empty',
+ 'parentFullTitle' => 'the body#konacha element',
+ 'status' => 'passed',
+ 'duration' => anything}}
+ end
+
+ let(:pending) do
+ {'event' => 'pending',
+ 'type' => 'test',
+ 'data' => {
+ 'title' => 'is pending',
+ 'fullTitle' => 'pending test is pending',
+ 'parentFullTitle' => 'pending test',
+ 'status' => 'pending'}}
+ end
+
+ it "passes along the right events" do
+ subject.reporter.should_receive(:process_mocha_event).with(suite)
+ subject.reporter.should_receive(:process_mocha_event).with(suite_end)
+ subject.reporter.should_receive(:process_mocha_event).with(test)
+ subject.reporter.should_receive(:process_mocha_event).with(failure)
+ subject.reporter.should_receive(:process_mocha_event).with(pass)
+ subject.reporter.should_receive(:process_mocha_event).with(pending)
+ subject.reporter.should_receive(:process_mocha_event).any_number_of_times
+ subject.run
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.