Skip to content

Commit

Permalink
Make use of doubles more consistent in github_repo_fetcher_spec
Browse files Browse the repository at this point in the history
We had some doubles that were duplicated in places, and an
unnecessary `let(:repo)` that was only used by one test.

`public_repo` became a method rather than an RSpec variable
because it gets used in several tests, and could trigger a
`RSpec::Mocks::ExpiredTestDoubleError`. Rather than litter each
usage with a `.dup` to duplicate the double, I've turned it into
a method so that it returns a new double each time.
  • Loading branch information
ChrisBAshton committed Jan 14, 2021
1 parent 9de2d4c commit 5acf5ad
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions spec/app/github_repo_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
)
end

let(:repo) do
double("stubbed repo", name: "some-repo", private_repo?: false, default_branch: "master")
end
let(:private_repo) { double("Private repo", private_repo?: true) }
let(:public_repo) { double("Public repo", private_repo?: false) }

def public_repo
double("Public repo", private_repo?: false, default_branch: "master")
end

describe "#repo" do
it "fetches a repo from cache if it exists" do
allow(CACHE).to receive(:fetch).with("all-repos", hash_including(:expires_in)) do
[OpenStruct.new({ name: "some-repo", default_branch: "master" })]
[public_repo]
end

repo = GitHubRepoFetcher.instance.repo("some-repo")
Expand Down Expand Up @@ -51,7 +51,7 @@ def readme_url
end

it "caches the first response" do
allow(GitHubRepoFetcher.instance).to receive(:repo).and_return(repo)
allow(GitHubRepoFetcher.instance).to receive(:repo).and_return(public_repo)
stubbed_request = stub_request(:get, readme_url)
.to_return(status: 200, body: "Foo")

Expand Down Expand Up @@ -121,9 +121,7 @@ def with_stubbed_client(temporary_client)
doc = double("doc", name: "foo.md", download_url: "foo_url", path: "docs/foo.md", html_url: "foo_html_url")

allow(GitHubRepoFetcher.instance).to receive(:latest_commit).and_return(commit)
allow(GitHubRepoFetcher.instance).to receive(:repo).with(repo_name).and_return(
double("repo", private_repo?: false, default_branch: "main"),
)
allow(GitHubRepoFetcher.instance).to receive(:repo).with(repo_name).and_return(public_repo)
allow(HTTP).to receive(:get).with(doc.download_url).and_return(doc_contents)
with_stubbed_client(double("Octokit::Client", contents: [doc])) do
expect(GitHubRepoFetcher.instance.docs(repo_name)).to eq([
Expand Down

0 comments on commit 5acf5ad

Please sign in to comment.