From b1b98adb2147af8f09d017c2270c1c9142b3fd35 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Tue, 23 Jun 2015 00:01:13 +0200 Subject: [PATCH] generate fixtures --- lib/tasks/tests.rake | 16 +++++++++++- .../webfinger_controller_spec.rb | 9 ++++--- spec/spec_helper.rb | 26 ++++++++++--------- spec/support/fixture_generation.rb | 16 ++++++++++++ 4 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 spec/support/fixture_generation.rb diff --git a/lib/tasks/tests.rake b/lib/tasks/tests.rake index ff6ef1e..8784c2b 100644 --- a/lib/tasks/tests.rake +++ b/lib/tasks/tests.rake @@ -1,9 +1,23 @@ namespace :ci do namespace :travis do task prepare_db: %w(db:create db:test:load) - task prepare: %w(prepare_db) + task :prepare_fixtures do + ENV["NO_COVERAGE"] = "true" + Rake::Task["spec:generate_fixtures"].invoke + ENV["NO_COVERAGE"] = "false" + end + task prepare: %w(prepare_db prepare_fixtures) desc "Run specs" task run: %w(prepare spec) end end + +if defined?(RSpec) + namespace :spec do + desc "Run all specs that generate fixtures for rspec" + RSpec::Core::RakeTask.new(generate_fixtures: "spec:prepare") do |t| + t.rspec_opts = ["--tag fixture"] + end + end +end diff --git a/spec/controllers/diaspora_federation/webfinger_controller_spec.rb b/spec/controllers/diaspora_federation/webfinger_controller_spec.rb index 183e5e9..3d2814c 100644 --- a/spec/controllers/diaspora_federation/webfinger_controller_spec.rb +++ b/spec/controllers/diaspora_federation/webfinger_controller_spec.rb @@ -8,9 +8,10 @@ module DiasporaFederation WebfingerController.instance_variable_set(:@host_meta_xml, nil) # clear cache end - it "succeeds" do + it "succeeds", fixture: true do get :host_meta expect(response).to be_success + save_fixture(response.body, "host-meta") end it "contains the webfinger-template" do @@ -36,9 +37,10 @@ module DiasporaFederation end describe "GET #legacy_webfinger" do - it "succeeds when the person exists" do + it "succeeds when the person exists", fixture: true do get :legacy_webfinger, "q" => "alice@localhost:3000" expect(response).to be_success + save_fixture(response.body, "legacy-webfinger") end it "succeeds with 'acct:' in the query when the person exists" do @@ -57,7 +59,8 @@ module DiasporaFederation end it "calls WebFinger::WebFinger.from_person" do - expect(WebFinger::WebFinger).to receive(:from_person).and_call_original + alice = Person.find_local_by_diaspora_handle("alice@localhost:3000") + expect(WebFinger::WebFinger).to receive(:from_person).with(alice.webfinger_hash).and_call_original get :legacy_webfinger, "q" => "acct:alice@localhost:3000" end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0c582be..6b40338 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,12 +1,17 @@ -require "simplecov" -require "simplecov-rcov" -SimpleCov.formatters = [ - SimpleCov::Formatter::HTMLFormatter, - SimpleCov::Formatter::RcovFormatter -] -SimpleCov.start do - add_filter "spec" - add_filter "test" +unless ENV["NO_COVERAGE"] == "true" + require "simplecov" + require "simplecov-rcov" + SimpleCov.formatters = [ + SimpleCov::Formatter::HTMLFormatter, + SimpleCov::Formatter::RcovFormatter + ] + SimpleCov.start do + add_filter "spec" + add_filter "test" + end + + require "codeclimate-test-reporter" + CodeClimate::TestReporter.start end ENV["RAILS_ENV"] ||= "test" @@ -17,9 +22,6 @@ require "rspec/rails" # load factory girl factories require "factories" -require "codeclimate-test-reporter" -CodeClimate::TestReporter.start - # Force fixture rebuild FileUtils.rm_f(Rails.root.join("tmp", "fixture_builder.yml")) diff --git a/spec/support/fixture_generation.rb b/spec/support/fixture_generation.rb new file mode 100644 index 0000000..1758017 --- /dev/null +++ b/spec/support/fixture_generation.rb @@ -0,0 +1,16 @@ +module FixtureGeneration + # Saves the markup to a fixture file using the given name + def save_fixture(markup, name, fixture_path=nil ) + fixture_path = Rails.root.join("tmp", "fixtures") unless fixture_path + Dir.mkdir(fixture_path) unless File.exist?(fixture_path) + + fixture_file = fixture_path.join("#{name}.fixture.html") + File.open(fixture_file, "w") do |file| + file.puts(markup) + end + end +end + +RSpec::Rails::ControllerExampleGroup.class_eval do + include FixtureGeneration +end