Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the README testing example to one that works #80

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,7 @@ context.
```ruby
describe AuthenticateUser do
describe "#call" do

let(:interactor) { AuthenticateUser.new(email: "john@example.com", password: "secret") }
let(:context) { interactor.context }
subject(:context) { AuthenticateUser.call(email: "john@example.com", password: "secret") }

context "when given valid credentials" do
let(:user) { double(:user, secret_token: "token") }
Expand All @@ -493,26 +491,14 @@ describe AuthenticateUser do
allow(User).to receive(:authenticate).with("john@example.com", "secret").and_return(user)
end

it "succeeds" do
interactor.call

expect(context).to be_a_success
end
it { is_expected.to be_a_success }

it "provides the user" do
expect {
interactor.call
}.to change {
context.user
}.from(nil).to(user)
expect(subject.user).to eq(user)
end

it "provides the user's secret token" do
expect {
interactor.call
}.to change {
context.token
}.from(nil).to("token")
expect(subject.token).to eq("token")
end
end

Expand All @@ -521,18 +507,10 @@ describe AuthenticateUser do
allow(User).to receive(:authenticate).with("john@example.com", "secret").and_return(nil)
end

it "fails" do
interactor.call

expect(context).to be_a_failure
end
it { is_expected.to be_a_failure }

it "provides a failure message" do
expect {
interactor.call
}.to change {
context.message
}.from(nil).to be_present
expect(subject.message).to be_present
end
end
end
Expand Down
52 changes: 52 additions & 0 deletions spec/readme_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class AuthenticateUser
include Interactor

def call
if user = User.authenticate(context.email, context.password)
context.user = user
context.token = user.secret_token
else
context.fail!(message: "authenticate_user.failure")
end
end
end

class User; end

describe "The testing example from the README" do
describe AuthenticateUser do
describe "#call" do
subject(:context) { AuthenticateUser.call(email: "john@example.com", password: "secret") }

context "when given valid credentials" do
let(:user) { double(:user, secret_token: "token") }

before do
allow(User).to receive(:authenticate).with("john@example.com", "secret").and_return(user)
end

it { is_expected.to be_a_success }

it "provides the user" do
expect(subject.user).to eq(user)
end

it "provides the user's secret token" do
expect(subject.token).to eq("token")
end
end

context "when given invalid credentials" do
before do
allow(User).to receive(:authenticate).with("john@example.com", "secret").and_return(nil)
end

it { is_expected.to be_a_failure }

it "provides a failure message" do
expect(subject.message).to_not be_nil # be_present requires activesupport
end
end
end
end
end