In Moq ist ein Mechanismus für den Umgang mit mehreren Schnittstellen integriert.
Sagen wir eine Schnittstelle IFoo und eine Implementierung der gleichen Foo haben. Wir haben auch ClientOne, die IFoo verwendet.
Wir haben dann eine Schnittstelle IFooBar haben: IFoo, eine Implementierung FooBar: Foo, IFooBar und ein ClientTwo die IFooBar verwendet.
Wenn ein End-to-End-Test für das System schaffen wir haben eine IFooBar, ClientOne und ClientTwo. Die Als <>() Funktion ermöglicht es uns, die Mock <IFooBar> als Mock <IFoo> zu verwenden.
public interface IFoo {
int Id { get; }
}
public class Foo : IFoo {
public int Id {
get { return 1; }
}
}
public interface IFooBar : IFoo {
string Name { get; }
}
public class FooBar : Foo, IFooBar {
public string Name {
get { return "AName"; }
}
}
public class ClientOne {
private readonly IFoo foo;
public ClientOne(IFoo foo) {
this.foo = foo;
}
public string Details {
get { return string.Format("Foo : {0}", foo.Id); }
}
}
public class ClientTwo {
private readonly IFooBar fooBar;
public ClientTwo(IFooBar fooBar) {
this.fooBar = fooBar;
}
public string Details {
get { return string.Format("Foo : {0}, Bar : {1}", fooBar.Id, fooBar.Name); }
}
}
[TestMethod]
public void TestUsingBothClients() {
var fooBarMock = new Mock<IFooBar>();
var fooMock = fooBarMock.As<IFoo>();
fooBarMock.SetupGet(mk => mk.Id).Returns(1);
fooBarMock.SetupGet(mk => mk.Name).Returns("AName");
var clientOne = new ClientOne(fooMock.Object);
var clientTwo = new ClientTwo(fooBarMock.Object);
Assert.AreEqual("Foo : 1", clientOne.Details);
Assert.AreEqual("Foo : 1, Bar : AName", clientTwo.Details);
}
Gibt es einen Zusammenhang zwischen 'IUserRepository' und' IGenericRepository'? Ist es erforderlich, dass ein Objekt, das 'IUserRepository' implementiert, auch 'IGenericRepository' 'implementiert? –
Wie sieht der Produktionscode aus, den Sie testen möchten? –