Use Moq framework instead of a concrete class to mock ICommandFactory in dotnet-run.UnitTests.

This commit is contained in:
Eric Erhardt 2016-05-13 20:24:22 -05:00
parent 6bf59ffde6
commit c84f41e749
2 changed files with 18 additions and 86 deletions

View file

@ -1,13 +1,12 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities;
using Moq;
using NuGet.Frameworks;
using Xunit;
@ -28,95 +27,27 @@ namespace Microsoft.DotNet.Tools.Run.Tests
.Should()
.Pass();
RunCommand runCommand = new RunCommand(new FailOnRedirectOutputCommandFactory());
// use MockBehavior.Strict to ensure the RunCommand doesn't call CaptureStdOut, ForwardStdOut, etc.
Mock<ICommand> failOnRedirectOutputCommand = new Mock<ICommand>(MockBehavior.Strict);
failOnRedirectOutputCommand
.Setup(c => c.Execute())
.Returns(new CommandResult(null, RunExitCode, null, null));
Mock<ICommandFactory> commandFactoryMock = new Mock<ICommandFactory>();
commandFactoryMock
.Setup(c => c.Create(
It.IsAny<string>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<NuGetFramework>(),
It.IsAny<string>()))
.Returns(failOnRedirectOutputCommand.Object);
RunCommand runCommand = new RunCommand(commandFactoryMock.Object);
runCommand.Project = instance.TestRoot;
runCommand.Start()
.Should()
.Be(RunExitCode);
}
private class FailOnRedirectOutputCommandFactory : ICommandFactory
{
public ICommand Create(string commandName, IEnumerable<string> args, NuGetFramework framework = null, string configuration = "Debug")
{
return new FailOnRedirectOutputCommand();
}
/// <summary>
/// A Command that will fail if a caller tries redirecting StdOut or StdErr.
/// </summary>
private class FailOnRedirectOutputCommand : ICommand
{
public CommandResult Execute()
{
return new CommandResult(null, RunExitCode, null, null);
}
public ICommand CaptureStdErr()
{
throw new NotSupportedException();
}
public ICommand CaptureStdOut()
{
throw new NotSupportedException();
}
public ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
throw new NotSupportedException();
}
public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
throw new NotSupportedException();
}
public ICommand OnErrorLine(Action<string> handler)
{
throw new NotSupportedException();
}
public ICommand OnOutputLine(Action<string> handler)
{
throw new NotSupportedException();
}
public string CommandArgs
{
get
{
throw new NotImplementedException();
}
}
public string CommandName
{
get
{
throw new NotImplementedException();
}
}
public CommandResolutionStrategy ResolutionStrategy
{
get
{
throw new NotImplementedException();
}
}
public ICommand EnvironmentVariable(string name, string value)
{
throw new NotImplementedException();
}
public ICommand WorkingDirectory(string projectDirectory)
{
throw new NotImplementedException();
}
}
}
}
}

View file

@ -13,6 +13,7 @@
"target": "project"
},
"xunit": "2.1.0",
"moq.netcore": "4.4.0-beta8",
"dotnet-test-xunit": "1.0.0-rc2-173361-36"
},
"frameworks": {