diff --git a/Source/Mockolate.Migration.Analyzers.CodeFixers/MoqCodeFixProvider.cs b/Source/Mockolate.Migration.Analyzers.CodeFixers/MoqCodeFixProvider.cs index f723db7..ae2ecb0 100644 --- a/Source/Mockolate.Migration.Analyzers.CodeFixers/MoqCodeFixProvider.cs +++ b/Source/Mockolate.Migration.Analyzers.CodeFixers/MoqCodeFixProvider.cs @@ -436,7 +436,7 @@ private static Dictionary count); + // returning different values / throwing on sequential calls + mock.Mock.Setup.DoSomething(It.IsAny()) + .Returns(true) + .Throws(new Exception("Error")) + .Returns(false); + /* ------ Async Methods ------ */ mock.Mock.Setup.DoSomethingAsync().ReturnsAsync(true); @@ -128,6 +134,12 @@ public async Task MoqCreation() int count = 1; mock.Setup(foo => foo.GetCount()).Returns(() => count); + // returning different values / throwing on sequential calls + mock.SetupSequence(foo => foo.DoSomething(Moq.It.IsAny())) + .Returns(true) + .Throws(new Exception("Error")) + .Returns(false); + /* ------ Async Methods ------ */ mock.Setup(foo => foo.DoSomethingAsync()).ReturnsAsync(true); diff --git a/Tests/Mockolate.Migration.Tests/MoqCodeFixProviderTests.SetupTests.cs b/Tests/Mockolate.Migration.Tests/MoqCodeFixProviderTests.SetupTests.cs index c6e2478..ab59633 100644 --- a/Tests/Mockolate.Migration.Tests/MoqCodeFixProviderTests.SetupTests.cs +++ b/Tests/Mockolate.Migration.Tests/MoqCodeFixProviderTests.SetupTests.cs @@ -496,5 +496,153 @@ public void Test() } } """); + + [Fact] + public async Task SetupSequence_Method_WithReturnsAndThrowsChain_MigratesToSetup() + => await Verifier.VerifyCodeFixAsync( + """ + using System; + using Moq; + + public interface IFoo { bool Dispense(string value, int count); } + + public class Tests + { + public void Test() + { + var mock = [|new Mock()|]; + mock.SetupSequence(f => f.Dispense(It.IsAny(), It.IsAny())) + .Returns(true) + .Throws(new Exception("Error")) + .Returns(false); + } + } + """, + """ + using System; + using Moq; + using Mockolate; + + public interface IFoo { bool Dispense(string value, int count); } + + public class Tests + { + public void Test() + { + var mock = IFoo.CreateMock(); + mock.Mock.Setup.Dispense(It.IsAny(), It.IsAny()) + .Returns(true) + .Throws(new Exception("Error")) + .Returns(false); + } + } + """); + + [Fact] + public async Task SetupSequence_Method_WithReturnsChain_MigratesToSetup() + => await Verifier.VerifyCodeFixAsync( + """ + using Moq; + + public interface IFoo { int GetCount(); } + + public class Tests + { + public void Test() + { + var mock = [|new Mock()|]; + mock.SetupSequence(f => f.GetCount()).Returns(1).Returns(2); + } + } + """, + """ + using Moq; + using Mockolate; + + public interface IFoo { int GetCount(); } + + public class Tests + { + public void Test() + { + var mock = IFoo.CreateMock(); + mock.Mock.Setup.GetCount().Returns(1).Returns(2); + } + } + """); + + [Fact] + public async Task SetupSequence_NestedMethod_UsesNavigationChain() + => await Verifier.VerifyCodeFixAsync( + """ + using System; + using Moq; + + public interface IBar { int GetCount(); } + public interface IFoo { IBar Child { get; } } + + public class Tests + { + public void Test() + { + var mock = [|new Mock()|]; + mock.SetupSequence(f => f.Child.GetCount()) + .Returns(1) + .Throws(); + } + } + """, + """ + using System; + using Moq; + using Mockolate; + + public interface IBar { int GetCount(); } + public interface IFoo { IBar Child { get; } } + + public class Tests + { + public void Test() + { + var mock = IFoo.CreateMock(); + mock.Child.Mock.GetCount() + .Returns(1) + .Throws(); + } + } + """); + + [Fact] + public async Task SetupSequence_Property_WithReturnsChain_MigratesToSetup() + => await Verifier.VerifyCodeFixAsync( + """ + using Moq; + + public interface IFoo { string Name { get; set; } } + + public class Tests + { + public void Test() + { + var mock = [|new Mock()|]; + mock.SetupSequence(f => f.Name).Returns("a").Returns("b"); + } + } + """, + """ + using Moq; + using Mockolate; + + public interface IFoo { string Name { get; set; } } + + public class Tests + { + public void Test() + { + var mock = IFoo.CreateMock(); + mock.Mock.Setup.Name.Returns("a").Returns("b"); + } + } + """); } }