Skip to content

Commit

Permalink
Try switching from let to method to prevent leaking double
Browse files Browse the repository at this point in the history
In theory, a `let` should create a new variable each time, but
we're seeing errors like:

```
     GitHubRepoFetcher#repo fetches a repo from GitHub if it doesn't exist in the cache
     Failure/Error: all_alphagov_repos.find { |repo| repo.name == repo_name } || raise("alphagov/#{repo_name} not found")
       #<Double "Public repo"> was originally created in one example but has leaked into another example and can no longer be used. rspec-mocks' doubles are designed to only last for one example, and you need to create a new one in each example you wish to use it for.
     # ./app/github_repo_fetcher.rb:8:in `block in repo'
     # ./app/github_repo_fetcher.rb:8:in `each'
     # ./app/github_repo_fetcher.rb:8:in `find'
     # ./app/github_repo_fetcher.rb:8:in `repo'
     # ./spec/app/github_repo_fetcher_spec.rb:27:in `block (3 levels) in <top (required)>'
```

We'll try switching to a method call so that it creates a new one
every time. See discussion:
#2918 (comment)
  • Loading branch information
ChrisBAshton committed Jan 20, 2021
1 parent 2f80db1 commit e59a95b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion spec/app/github_repo_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
end

let(:private_repo) { double("Private repo", private_repo?: true) }
let(:public_repo) { double("Public repo", private_repo?: false, default_branch: "master") }
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
Expand Down

0 comments on commit e59a95b

Please sign in to comment.