From 3c793dc95bf9bad6be0f9bd9d0fecadf6c9b4784 Mon Sep 17 00:00:00 2001 From: Steve Clarke Date: Wed, 4 Mar 2026 20:44:52 -0330 Subject: [PATCH] test: add network failure and edge case test coverage Add tests for timeout, connection failure, HTTP 500, and empty response body across all three providers (BibleGateway, NetBible, OurManna). Add invalid argument tests for BibleGateway.new with nil, String, and Integer. Extract shared example group to DRY up fallback assertions. Also add bin/standardrb binstub. Closes #14 --- bin/standardrb | 27 +++++++++++++++++ spec/lib/votd/bible_gateway_spec.rb | 45 +++++++++++++++++++++++------ spec/lib/votd/netbible_spec.rb | 27 +++++++++++++---- spec/lib/votd/ourmanna_spec.rb | 27 +++++++++++++---- spec/spec_helper.rb | 8 +++++ 5 files changed, 113 insertions(+), 21 deletions(-) create mode 100755 bin/standardrb diff --git a/bin/standardrb b/bin/standardrb new file mode 100755 index 0000000..b329561 --- /dev/null +++ b/bin/standardrb @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'standardrb' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +bundle_binstub = File.expand_path("bundle", __dir__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300).include?("This file was generated by Bundler") + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("standard", "standardrb") diff --git a/spec/lib/votd/bible_gateway_spec.rb b/spec/lib/votd/bible_gateway_spec.rb index cb13108..aeea81c 100644 --- a/spec/lib/votd/bible_gateway_spec.rb +++ b/spec/lib/votd/bible_gateway_spec.rb @@ -102,15 +102,30 @@ end end - context "When an error occurrs" do - before do - fake_a_broken_uri(uri_regex) + context "When an error occurs" do + context "with a malformed response body" do + before { fake_a_broken_uri(uri_regex) } + include_examples "falls back to defaults" + end + + context "with a network timeout" do + before { stub_request(:get, uri_regex).to_timeout } + include_examples "falls back to defaults" + end + + context "with a connection failure" do + before { stub_request(:get, uri_regex).to_raise(SocketError) } + include_examples "falls back to defaults" + end + + context "with an HTTP 500 response" do + before { stub_request(:get, uri_regex).to_return(status: 500, body: "Internal Server Error") } + include_examples "falls back to defaults" end - it "falls back to default VotD values" do - expect(votd.version).to eq Votd::Base::DEFAULT_BIBLE_VERSION - expect(votd.reference).to eq Votd::Base::DEFAULT_BIBLE_REFERENCE - expect(votd.text).to eq Votd::Base::DEFAULT_BIBLE_TEXT + context "with an empty response body" do + before { stub_request(:get, uri_regex).to_return(body: "") } + include_examples "falls back to defaults" end end @@ -152,10 +167,22 @@ expect(votd_nlt.link).to eq "https://www.biblegateway.com/passage/?search=Colossians+3%3A16&version=51" end - context "with an invalid version code" do - it "throws an error" do + context "with an invalid version argument" do + it "raises InvalidBibleVersion for an unknown symbol" do expect { Votd::BibleGateway.new(:foo) }.to raise_error(Votd::InvalidBibleVersion) end + + it "raises InvalidBibleVersion for nil" do + expect { Votd::BibleGateway.new(nil) }.to raise_error(Votd::InvalidBibleVersion) + end + + it "raises InvalidBibleVersion for a String" do + expect { Votd::BibleGateway.new("niv") }.to raise_error(Votd::InvalidBibleVersion) + end + + it "raises InvalidBibleVersion for an Integer" do + expect { Votd::BibleGateway.new(31) }.to raise_error(Votd::InvalidBibleVersion) + end end end end diff --git a/spec/lib/votd/netbible_spec.rb b/spec/lib/votd/netbible_spec.rb index 592574a..1e2cdd4 100644 --- a/spec/lib/votd/netbible_spec.rb +++ b/spec/lib/votd/netbible_spec.rb @@ -112,14 +112,29 @@ end context "When an error occurs" do - before do - fake_a_broken_uri(Votd::NetBible::ENDPOINT_URL) + context "with a malformed response body" do + before { fake_a_broken_uri(Votd::NetBible::ENDPOINT_URL) } + include_examples "falls back to defaults" + end + + context "with a network timeout" do + before { stub_request(:get, Votd::NetBible::ENDPOINT_URL).to_timeout } + include_examples "falls back to defaults" + end + + context "with a connection failure" do + before { stub_request(:get, Votd::NetBible::ENDPOINT_URL).to_raise(SocketError) } + include_examples "falls back to defaults" + end + + context "with an HTTP 500 response" do + before { stub_request(:get, Votd::NetBible::ENDPOINT_URL).to_return(status: 500, body: "Internal Server Error") } + include_examples "falls back to defaults" end - it "falls back to default VotD values" do - expect(votd.version).to eq Votd::Base::DEFAULT_BIBLE_VERSION - expect(votd.reference).to eq Votd::Base::DEFAULT_BIBLE_REFERENCE - expect(votd.text).to eq Votd::Base::DEFAULT_BIBLE_TEXT + context "with an empty response body" do + before { stub_request(:get, Votd::NetBible::ENDPOINT_URL).to_return(body: "") } + include_examples "falls back to defaults" end end diff --git a/spec/lib/votd/ourmanna_spec.rb b/spec/lib/votd/ourmanna_spec.rb index 7fba9b9..1a4a0c3 100644 --- a/spec/lib/votd/ourmanna_spec.rb +++ b/spec/lib/votd/ourmanna_spec.rb @@ -102,14 +102,29 @@ end context "When an error occurs" do - before do - fake_a_broken_uri(Votd::OurManna::ENDPOINT_URL) + context "with a malformed response body" do + before { fake_a_broken_uri(Votd::OurManna::ENDPOINT_URL) } + include_examples "falls back to defaults" + end + + context "with a network timeout" do + before { stub_request(:get, Votd::OurManna::ENDPOINT_URL).to_timeout } + include_examples "falls back to defaults" + end + + context "with a connection failure" do + before { stub_request(:get, Votd::OurManna::ENDPOINT_URL).to_raise(SocketError) } + include_examples "falls back to defaults" + end + + context "with an HTTP 500 response" do + before { stub_request(:get, Votd::OurManna::ENDPOINT_URL).to_return(status: 500, body: "Internal Server Error") } + include_examples "falls back to defaults" end - it "falls back to default VotD values" do - expect(votd.version).to eq Votd::Base::DEFAULT_BIBLE_VERSION - expect(votd.reference).to eq Votd::Base::DEFAULT_BIBLE_REFERENCE - expect(votd.text).to eq Votd::Base::DEFAULT_BIBLE_TEXT + context "with an empty response body" do + before { stub_request(:get, Votd::OurManna::ENDPOINT_URL).to_return(body: "") } + include_examples "falls back to defaults" end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6944f84..8be43dd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,3 +24,11 @@ def fake_a_uri(uri, fixture_path) def fake_a_broken_uri(uri) stub_request(:get, uri).to_return(body: "Oopsies") end + +RSpec.shared_examples "falls back to defaults" do + it "falls back to default VotD values" do + expect(votd.version).to eq Votd::Base::DEFAULT_BIBLE_VERSION + expect(votd.reference).to eq Votd::Base::DEFAULT_BIBLE_REFERENCE + expect(votd.text).to eq Votd::Base::DEFAULT_BIBLE_TEXT + end +end