From 32591ce3fcd5cdacf173a68c9d60cf021f30f276 Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 6 Mar 2015 15:06:24 -0600 Subject: [PATCH 1/3] Add the README testing example as a spec The example of how to testing Interactors in the README does not work in the current version of `interactor`. This is a huge barrier to entry for anyone looking to use the library. We should update the README to indicate how tests should actually be done. Note: This commit is just intended to add the spec to show that it fails. I will add a second commit in a moment to show the method that I use to test my Interactors. --- spec/readme_spec.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 spec/readme_spec.rb diff --git a/spec/readme_spec.rb b/spec/readme_spec.rb new file mode 100644 index 0000000..72d6014 --- /dev/null +++ b/spec/readme_spec.rb @@ -0,0 +1,73 @@ +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 + let(:interactor) { AuthenticateUser.new(email: "john@example.com", password: "secret") } + let(:context) { interactor.context } + + 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 "succeeds" do + interactor.call + + expect(context).to be_a_success + end + + it "provides the user" do + expect { + interactor.call + }.to change { + context.user + }.from(nil).to(user) + end + + it "provides the user's secret token" do + expect { + interactor.call + }.to change { + context.token + }.from(nil).to("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 "fails" do + interactor.call + + expect(context).to be_a_failure + end + + it "provides a failure message" do + expect { + interactor.call + }.to change { + context.message + }.from(nil).to be_present + end + end + end + end +end From 02562a5bc753e1a9907db6f85f6a23a052630228 Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 6 Mar 2015 15:23:46 -0600 Subject: [PATCH 2/3] Update the README example to a working version --- README.md | 34 ++++++---------------------------- spec/readme_spec.rb | 33 ++++++--------------------------- 2 files changed, 12 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index c4d1e6f..cfb7b28 100644 --- a/README.md +++ b/README.md @@ -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") } @@ -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 @@ -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_not be_present end end end diff --git a/spec/readme_spec.rb b/spec/readme_spec.rb index 72d6014..56d362e 100644 --- a/spec/readme_spec.rb +++ b/spec/readme_spec.rb @@ -16,8 +16,7 @@ class User; end describe "The testing example from the README" do 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") } @@ -26,26 +25,14 @@ class User; end 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 @@ -54,18 +41,10 @@ class User; end 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_not be_nil # be_present requires activesupport end end end From ff5ef4d8c2011ea277fa81db60ae1d2dca887420 Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Tue, 5 May 2015 09:34:01 -0500 Subject: [PATCH 3/3] Fix typo in new README testing example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cfb7b28..2bcffba 100644 --- a/README.md +++ b/README.md @@ -510,7 +510,7 @@ describe AuthenticateUser do it { is_expected.to be_a_failure } it "provides a failure message" do - expect(subject.message).to_not be_present + expect(subject.message).to be_present end end end