From 31b959dbb75c063754f98489e4da9bf88e173cec Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Mon, 5 Nov 2018 22:07:17 +0100 Subject: [PATCH] Replace FakeItEasy with custom class (#959) --- Tests/Net45.Specs/Net45.Specs.csproj | 1 - Tests/Net47.Specs/Net47.Specs.csproj | 1 - Tests/NetCore.Specs/NetCore.Specs.csproj | 1 - Tests/NetCore20.Specs/NetCore20.Specs.csproj | 3 +- .../NetStandard13.Specs.csproj | 1 - Tests/Shared.Specs/ExceptionAssertionSpecs.cs | 139 +++++++++--------- Tests/Shared.Specs/ThrowAssertionsSpecs.cs | 6 +- 7 files changed, 76 insertions(+), 76 deletions(-) diff --git a/Tests/Net45.Specs/Net45.Specs.csproj b/Tests/Net45.Specs/Net45.Specs.csproj index b541b2fc55..3fe100e09d 100644 --- a/Tests/Net45.Specs/Net45.Specs.csproj +++ b/Tests/Net45.Specs/Net45.Specs.csproj @@ -11,7 +11,6 @@ - diff --git a/Tests/Net47.Specs/Net47.Specs.csproj b/Tests/Net47.Specs/Net47.Specs.csproj index 3f8ba64cc0..7e8f07cd36 100644 --- a/Tests/Net47.Specs/Net47.Specs.csproj +++ b/Tests/Net47.Specs/Net47.Specs.csproj @@ -11,7 +11,6 @@ - diff --git a/Tests/NetCore.Specs/NetCore.Specs.csproj b/Tests/NetCore.Specs/NetCore.Specs.csproj index 952598ddc2..868e329a4b 100644 --- a/Tests/NetCore.Specs/NetCore.Specs.csproj +++ b/Tests/NetCore.Specs/NetCore.Specs.csproj @@ -7,7 +7,6 @@ 7.2 - diff --git a/Tests/NetCore20.Specs/NetCore20.Specs.csproj b/Tests/NetCore20.Specs/NetCore20.Specs.csproj index b3447cf607..c004901325 100644 --- a/Tests/NetCore20.Specs/NetCore20.Specs.csproj +++ b/Tests/NetCore20.Specs/NetCore20.Specs.csproj @@ -10,7 +10,6 @@ NU1701 - @@ -25,4 +24,4 @@ - \ No newline at end of file + diff --git a/Tests/NetStandard13.Specs/NetStandard13.Specs.csproj b/Tests/NetStandard13.Specs/NetStandard13.Specs.csproj index 0bd8d19f94..d50239db5d 100644 --- a/Tests/NetStandard13.Specs/NetStandard13.Specs.csproj +++ b/Tests/NetStandard13.Specs/NetStandard13.Specs.csproj @@ -8,7 +8,6 @@ 7.2 - diff --git a/Tests/Shared.Specs/ExceptionAssertionSpecs.cs b/Tests/Shared.Specs/ExceptionAssertionSpecs.cs index 1d62c5a807..40f2853525 100644 --- a/Tests/Shared.Specs/ExceptionAssertionSpecs.cs +++ b/Tests/Shared.Specs/ExceptionAssertionSpecs.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using FakeItEasy; using Xunit; using Xunit.Sdk; @@ -21,7 +20,7 @@ public void ThrowExactly_when_subject_throws_subclass_of_expected_exception_it_s //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - act.Should().ThrowExactly("because {0} should do that", "IFoo.Do"); + act.Should().ThrowExactly("because {0} should do that", "Does.Do"); throw new XunitException("This point should not be reached."); } @@ -30,7 +29,7 @@ public void ThrowExactly_when_subject_throws_subclass_of_expected_exception_it_s //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- - ex.Message.Should().Match("Expected type to be System.ArgumentException because IFoo.Do should do that, but found System.ArgumentNullException."); + ex.Message.Should().Match("Expected type to be System.ArgumentException because Does.Do should do that, but found System.ArgumentNullException."); } } @@ -56,8 +55,7 @@ public void When_subject_throws_expected_exception_with_an_expected_message_it_s //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new InvalidOperationException("some message")); + Does testSubject = Does.Throw(new InvalidOperationException("some message")); //----------------------------------------------------------------------------------------------------------- // Act / Assert @@ -71,8 +69,7 @@ public void When_subject_throws_expected_exception_but_with_unexpected_message_i //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new InvalidOperationException("some")); + Does testSubject = Does.Throw(new InvalidOperationException("some")); try { @@ -102,8 +99,7 @@ public void When_subject_throws_expected_exception_with_message_starting_with_ex //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new InvalidOperationException("expected message")); + Does testSubject = Does.Throw(new InvalidOperationException("expected message")); //----------------------------------------------------------------------------------------------------------- // Act @@ -123,8 +119,7 @@ public void When_subject_throws_expected_exception_with_message_that_does_not_st //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new InvalidOperationException("OxpectOd message")); + Does testSubject = Does.Throw(new InvalidOperationException("OxpectOd message")); //----------------------------------------------------------------------------------------------------------- // Act @@ -147,8 +142,7 @@ public void When_subject_throws_expected_exception_with_message_starting_with_ex //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new InvalidOperationException("Expected Message")); + Does testSubject = Does.Throw(new InvalidOperationException("Expected Message")); //----------------------------------------------------------------------------------------------------------- // Act @@ -168,8 +162,7 @@ public void When_subject_throws_expected_exception_with_message_that_does_not_st //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new InvalidOperationException("OxpectOd message")); + Does testSubject = Does.Throw(new InvalidOperationException("OxpectOd message")); //----------------------------------------------------------------------------------------------------------- // Act @@ -192,8 +185,7 @@ public void When_subject_throws_some_exception_with_unexpected_message_it_should //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo subjectThatThrows = A.Fake(); - A.CallTo(() => subjectThatThrows.Do()).Throws(new InvalidOperationException("message1")); + Does subjectThatThrows = Does.Throw(new InvalidOperationException("message1")); try { @@ -223,8 +215,7 @@ public void When_subject_throws_some_exception_with_an_empty_message_it_should_t //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo subjectThatThrows = A.Fake(); - A.CallTo(() => subjectThatThrows.Do()).Throws(new InvalidOperationException("")); + Does subjectThatThrows = Does.Throw(new InvalidOperationException("")); try { @@ -254,9 +245,7 @@ public void When_subject_throws_some_exception_with_message_which_contains_compl //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo subjectThatThrows = A.Fake(); - A.CallTo(() => subjectThatThrows.Do(A.Ignored)) - .Throws(new ArgumentNullException("someParam", "message2")); + Does subjectThatThrows = Does.Throw(new ArgumentNullException("someParam", "message2")); try { @@ -288,12 +277,12 @@ public void When_no_exception_was_thrown_but_one_was_expected_it_should_clearly_ //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); + Does testSubject = Does.NotThrow(); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- - testSubject.Invoking(x => x.Do()).Should().Throw("because {0} should do that", "IFoo.Do"); + testSubject.Invoking(x => x.Do()).Should().Throw("because {0} should do that", "Does.Do"); throw new XunitException("This point should not be reached"); } @@ -303,7 +292,7 @@ public void When_no_exception_was_thrown_but_one_was_expected_it_should_clearly_ // Assert //----------------------------------------------------------------------------------------------------------- ex.Message.Should().Be( - "Expected a to be thrown because IFoo.Do should do that, but no exception was thrown."); + "Expected a to be thrown because Does.Do should do that, but no exception was thrown."); } } @@ -315,8 +304,7 @@ public void When_subject_throws_another_exception_than_expected_it_should_includ //----------------------------------------------------------------------------------------------------------- var actualException = new ArgumentException(); - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(actualException); + Does testSubject = Does.Throw(actualException); try { @@ -325,7 +313,7 @@ public void When_subject_throws_another_exception_than_expected_it_should_includ //----------------------------------------------------------------------------------------------------------- testSubject .Invoking(x => x.Do()) - .Should().Throw("because {0} should throw that one", "IFoo.Do"); + .Should().Throw("because {0} should throw that one", "Does.Do"); throw new XunitException("This point should not be reached"); } @@ -335,7 +323,7 @@ public void When_subject_throws_another_exception_than_expected_it_should_includ // Assert //----------------------------------------------------------------------------------------------------------- ex.Message.Should().StartWith( - "Expected a to be thrown because IFoo.Do should throw that one, but found a :"); + "Expected a to be thrown because Does.Do should throw that one, but found a :"); ex.Message.Should().Contain(actualException.Message); } @@ -347,9 +335,7 @@ public void When_subject_throws_exception_with_message_with_braces_but_a_differe //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo subjectThatThrows = A.Fake(); - A.CallTo(() => subjectThatThrows.Do(A.Ignored)) - .Throws(new Exception("message with {}")); + Does subjectThatThrows = Does.Throw(new Exception("message with {}")); try { @@ -379,10 +365,7 @@ public void When_asserting_with_an_aggregate_exception_type_the_asserts_should_o //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()) - .Throws(new AggregateException("Outer Message", - new Exception("Inner Message"))); + Does testSubject = Does.Throw(new AggregateException("Outer Message", new Exception("Inner Message"))); //----------------------------------------------------------------------------------------------------------- // Act @@ -408,8 +391,7 @@ public void When_subject_throws_an_exception_with_the_expected_inner_exception_i //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new Exception("", new ArgumentException())); + Does testSubject = Does.Throw(new Exception("", new ArgumentException())); //----------------------------------------------------------------------------------------------------------- // Act / Assert @@ -527,8 +509,7 @@ public void When_subject_throws_an_exception_with_an_unexpected_inner_exception_ //----------------------------------------------------------------------------------------------------------- var innerException = new NullReferenceException(); - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new Exception("", innerException)); + Does testSubject = Does.Throw(new Exception("", innerException)); try { @@ -538,7 +519,7 @@ public void When_subject_throws_an_exception_with_an_unexpected_inner_exception_ testSubject .Invoking(x => x.Do()) .Should().Throw() - .WithInnerException("because {0} should do just that", "IFoo.Do"); + .WithInnerException("because {0} should do just that", "Does.Do"); throw new XunitException("This point should not be reached"); } @@ -548,7 +529,7 @@ public void When_subject_throws_an_exception_with_an_unexpected_inner_exception_ // Assert //----------------------------------------------------------------------------------------------------------- exc.Message.Should().StartWith( - "Expected inner System.ArgumentException because IFoo.Do should do just that, but found System.NullReferenceException"); + "Expected inner System.ArgumentException because Does.Do should do just that, but found System.NullReferenceException"); exc.Message.Should().Contain(innerException.Message); } @@ -559,8 +540,7 @@ public void When_subject_throws_an_exception_without_expected_inner_exception_it { try { - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new Exception("")); + Does testSubject = Does.Throw(); testSubject.Invoking(x => x.Do()).Should().Throw() .WithInnerException(); @@ -581,18 +561,17 @@ public void { try { - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new Exception("")); + Does testSubject = Does.Throw(); testSubject.Invoking(x => x.Do()).Should().Throw() - .WithInnerException("because {0} should do that", "IFoo.Do"); + .WithInnerException("because {0} should do that", "Does.Do"); throw new XunitException("This point should not be reached"); } catch (XunitException ex) { ex.Message.Should().Be( - "Expected inner System.InvalidOperationException because IFoo.Do should do that, but the thrown exception has no inner exception."); + "Expected inner System.InvalidOperationException because Does.Do should do that, but the thrown exception has no inner exception."); } } @@ -625,8 +604,7 @@ public void When_getting_value_of_property_of_thrown_exception_it_should_return_ // Arrange //----------------------------------------------------------------------------------------------------------- const string SomeParamNameValue = "param"; - var target = A.Fake(); - A.CallTo(() => target.Do()).Throws(new ExceptionWithProperties(SomeParamNameValue)); + Does target = Does.Throw(new ExceptionWithProperties(SomeParamNameValue)); //----------------------------------------------------------------------------------------------------------- // Act @@ -645,9 +623,7 @@ public void When_validating_a_subject_against_multiple_conditions_it_should_supp //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws( - new InvalidOperationException("message", new ArgumentException("inner message"))); + Does testSubject = Does.Throw(new InvalidOperationException("message", new ArgumentException("inner message"))); //----------------------------------------------------------------------------------------------------------- // Act / Assert @@ -774,9 +750,7 @@ public void //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()) - .Throws(new AggregateException(new Exception(), new Exception())); + Does testSubject = Does.Throw(new AggregateException(new Exception(), new Exception())); Action throwingMethod = testSubject.Do; //----------------------------------------------------------------------------------------------------------- @@ -823,8 +797,7 @@ public void When_a_specific_exception_should_not_be_thrown_but_it_was_it_should_ //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - var foo = A.Fake(); - A.CallTo(() => foo.Do()).Throws(new ArgumentException("An exception was forced")); + Does foo = Does.Throw(new ArgumentException("An exception was forced")); //----------------------------------------------------------------------------------------------------------- // Act @@ -847,8 +820,7 @@ public void When_a_specific_exception_should_not_be_thrown_but_another_was_it_sh //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - var foo = A.Fake(); - A.CallTo(() => foo.Do()).Throws(new ArgumentException()); + Does foo = Does.Throw(); //----------------------------------------------------------------------------------------------------------- // Act / Assert @@ -862,8 +834,7 @@ public void When_no_exception_should_be_thrown_but_it_was_it_should_throw() //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - var foo = A.Fake(); - A.CallTo(() => foo.Do()).Throws(new ArgumentException("An exception was forced")); + Does foo = Does.Throw(new ArgumentException("An exception was forced")); //----------------------------------------------------------------------------------------------------------- // Act @@ -885,7 +856,7 @@ public void When_no_exception_should_be_thrown_and_none_was_it_should_not_throw( //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - var foo = A.Fake(); + Does foo = Does.NotThrow(); //----------------------------------------------------------------------------------------------------------- // Act / Assert @@ -908,11 +879,47 @@ public void Throw() } } - public interface IFoo + public abstract class Does { - void Do(); + public abstract void Do(); - void Do(string someParam); + public abstract void Do(string someParam); + + public static Does Throw(TException exception) + where TException : Exception + { + return new DoesThrow(exception); + } + + public static Does Throw() + where TException : Exception, new() + { + return Throw(new TException()); + } + + public static Does NotThrow() => new DoesNotThrow(); + + private class DoesThrow : Does + where TException : Exception + { + private readonly TException exception; + + public DoesThrow(TException exception) + { + this.exception = exception; + } + + public override void Do() => throw exception; + + public override void Do(string someParam) => throw exception; + } + + private class DoesNotThrow : Does + { + public override void Do() { } + + public override void Do(string someParam) { } + } } internal class ExceptionWithProperties : Exception diff --git a/Tests/Shared.Specs/ThrowAssertionsSpecs.cs b/Tests/Shared.Specs/ThrowAssertionsSpecs.cs index 6fd5058442..6a0fe0d5f3 100644 --- a/Tests/Shared.Specs/ThrowAssertionsSpecs.cs +++ b/Tests/Shared.Specs/ThrowAssertionsSpecs.cs @@ -1,5 +1,4 @@ using System; -using FakeItEasy; using Xunit; using Xunit.Sdk; @@ -13,8 +12,7 @@ public void When_subject_throws_expected_exception_it_should_not_do_anything() //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- - IFoo testSubject = A.Fake(); - A.CallTo(() => testSubject.Do()).Throws(new InvalidOperationException()); + Does testSubject = Does.Throw(); //----------------------------------------------------------------------------------------------------------- // Act / Assert @@ -41,7 +39,7 @@ public void When_subject_does_not_throw_exception_but_one_was_expected_it_should { try { - IFoo testSubject = A.Fake(); + Does testSubject = Does.NotThrow(); testSubject.Invoking(x => x.Do()).Should().Throw();