Видите ли, интерфейс IFile
и класс LocalFile
- это разные типы. Поэтому мы не можем заставить Mock<IFile>
воспроизводить реализацию из LocalFile
по умолчанию. Чтобы ваш тест работал, вы можете добавить следующий Callback
звонок:
public Mock<IFile> GetNewFile(string name)
{
Mock<IFile> file = new Mock<IFile>();
file.CallBase = true;
file.Setup(f => f.Name).Returns(name);
file.Setup(f => f.Path).Returns(@"C:\Folder\" + name);
file.Setup(f => f.AppendName()).Callback(() => file.Setup(f => f.Name).Returns(() =>
{
var localFile = new LocalFile();
localFile.AppendName();
return localFile.Name;
// or just return "AppendedDocument"
}));
return file;
}
Пример работает, однако, это не то, что вам нужно, я полагаю. Даже для Mock<LocalFile>
с CallBase = true
и public virtual public string Name { get; set; }
вам нужно как-то очистить настройку свойства. Насколько я знаю, Moq не позволяет этого. Я могу ошибаться. Вы можете попробовать следующий обходной путь, основанный на той же идее:
public class GenericTests
{
[Test]
public void TestMethod1()
{
IFile newFile = GetNewFile("Document");
newFile.AppendName();
Assert.AreEqual("AppendedDocument", newFile.Name);
}
public IFile GetNewFile(string name)
{
Mock<LocalFile> file = new Mock<LocalFile>();
file.CallBase = true;
file.Object.AppendName();
// remember expected result before setting up the mock
var appendDoc = file.Object.Name;
file.Setup(f => f.Name).Returns(name);
file.Setup(f => f.Path).Returns(@"C:\Folder\" + name);
file.Setup(f => f.AppendName())
// change property behavior after call of AppendName
.Callback(() => file.Setup(f => f.Name).Returns(appendDoc));
return file.Object;
}
}
public interface IFile
{
string Name { get; set; }
string Path { get; set; }
void AppendName();
}
public class LocalFile : IFile
{
public virtual string Name { get; set; }
public virtual string Path { get; set; }
public virtual void AppendName()
{
this.Name = "AppendedDocument";
}
}
Я немного изменил ваш пример. GetNewFile
теперь возвращает IFile
экземпляр, и члены LocalFile
стали виртуальными. Надеюсь, это поможет.