public void TestCreateMockxUnit()
        {
            var test = @"
            using Microsoft.VisualStudio.TestTools.UnitTesting;
            using TestMockProjectTest;
            
            namespace TestMockUnitTests
            {
                public class UnitTest2
                {
                    private IService sut;
            
                    public UnitTest2()
                    {
                        sut = new Service();
                    }
            
                    [Fact]
                    public void TestMethod1()
                    {
                        sut.DoSomething(2);
                    }
                }
            }
            
            namespace TestMockProjectTest
            {
            
                public interface IService
                {
                    void DoSomething(int doInt);
                }
            
                public interface ISubService
                {
                    void DoSubSomething(int doInt);
                }
            
                public class Service : IService
                {
                    private readonly ISubService _subService;
            
                    public Service(ISubService subService)
                    {
                        _subService = subService;
                    }
            
                    public void DoSomething(int doInt)
                    {
                        _subService.DoSubSomething(doInt);
                    }
                }
            }";
            var expected = new DiagnosticResult
            {
                Id = TestInitializeDiagnosticAnalyzer.DiagnosticId,
                Message = $"Can be mocked",
                Severity = DiagnosticSeverity.Info,
                Locations =
                    new[] {
                            new DiagnosticResultLocation("Test0.cs", 13, 25)
                        }
            };

            VerifyCSharpDiagnostic(test, expected);

            var fixtest = @"
            using Microsoft.VisualStudio.TestTools.UnitTesting;
            using TestMockProjectTest;
            
            namespace TestMockUnitTests
            {
                public class UnitTest2
                {
                    private IService sut;
        private Mock<ISubService> subServiceMock;

        public UnitTest2()
                    {
            subServiceMock = new Mock<ISubService>();

            sut = new Service(subServiceMock.Object);
                    }
            
                    [Fact]
                    public void TestMethod1()
                    {
                        sut.DoSomething(2);
                    }
                }
            }
            
            namespace TestMockProjectTest
            {
            
                public interface IService
                {
                    void DoSomething(int doInt);
                }
            
                public interface ISubService
                {
                    void DoSubSomething(int doInt);
                }
            
                public class Service : IService
                {
                    private readonly ISubService _subService;
            
                    public Service(ISubService subService)
                    {
                        _subService = subService;
                    }
            
                    public void DoSomething(int doInt)
                    {
                        _subService.DoSubSomething(doInt);
                    }
                }
            }";
            VerifyCSharpFix(test, fixtest);
        }
Example #2
0
        public void TestSetupWithoutReturns()
        {
            var test = @"
            using Microsoft.VisualStudio.TestTools.UnitTesting;
            using TestMockProjectTest;
            
            namespace TestMockUnitTests
            {
                [TestClass]
                public class UnitTest2
                {
                    private IService sut;
                    private Mock<ISubService> subServiceMock;

        [TestInitialize]
                    public void Init()
                    {
            subServiceMock = new Mock<ISubService>();
            sut = new Service(subServiceMock.Object);
                    }
            
                    [TestMethod]
                    public void TestMethod1()
                    {
                        sut.DoSomething(2);
                    }
                }
            }
            
            namespace TestMockProjectTest
            {
            
                public interface IService
                {
                    void DoSomething(int doInt);
                }
            
                public interface ISubService
                {
                    void DoSubSomething(int doInt);
                }
            
                public class Service : IService
                {
                    private readonly ISubService _subService;
            
                    public Service(ISubService subService)
                    {
                        _subService = subService;
                    }
            
                    public void DoSomething(int doInt)
                    {
                        _subService.DoSubSomething(doInt);
                    }
                }
            }";
            var expected = new DiagnosticResult
            {
                Id = TestMethodDiagnosticAnalyzer.DiagnosticId,
                Message = $"Can be mocked",
                Severity = DiagnosticSeverity.Warning,
                Locations =
                    new[] {
                            new DiagnosticResultLocation("Test0.cs", 23, 25)
                        }
            };

            VerifyCSharpDiagnostic(test, expected);

            var fixtest = @"
            using Microsoft.VisualStudio.TestTools.UnitTesting;
            using TestMockProjectTest;
            
            namespace TestMockUnitTests
            {
                [TestClass]
                public class UnitTest2
                {
                    private IService sut;
                    private Mock<ISubService> subServiceMock;

        [TestInitialize]
                    public void Init()
                    {
            subServiceMock = new Mock<ISubService>();
            sut = new Service(subServiceMock.Object);
                    }
            
                    [TestMethod]
                    public void TestMethod1()
                    {
            subServiceMock.Setup(x => x.DoSubSomething(It.Is<int>(doInt => doInt == default(int))));

            sut.DoSomething(2);
            subServiceMock.VerifyAll();
        }
                }
            }
            
            namespace TestMockProjectTest
            {
            
                public interface IService
                {
                    void DoSomething(int doInt);
                }
            
                public interface ISubService
                {
                    void DoSubSomething(int doInt);
                }
            
                public class Service : IService
                {
                    private readonly ISubService _subService;
            
                    public Service(ISubService subService)
                    {
                        _subService = subService;
                    }
            
                    public void DoSomething(int doInt)
                    {
                        _subService.DoSubSomething(doInt);
                    }
                }
            }";
            VerifyCSharpFix(test, fixtest);
        }