我有以下界面:
public interface IOuputDestination
{
void Write(String s);
}
Run Code Online (Sandbox Code Playgroud)
在我的单元测试中,我这样模仿它:
var outputDestination = A.Fake<IOutputDestination>();
Run Code Online (Sandbox Code Playgroud)
我想要做的是拦截Write方法,以便它使用我在测试中定义的自定义实现.像这样的东西:
String output = "";
A.CallTo(() => outputDestination.Write(A<String>.Ignored)).Action(s => output += s);
//Is something like this even possible ? ----^
Run Code Online (Sandbox Code Playgroud)
这里的Action
方法不存在,但我想要发生的是传入的任何参数都outputDestination.Write
被重定向到我的自定义操作.这可能是使用FakeItEasy吗?如果没有,是否有另一个允许这种行为的模拟框架?
我试图弄清楚如何使用HttpClient使用FakeItEasy,给出以下代码:
public Foo(string key, HttpClient httpClient = null)
{ .. }
public void DoGet()
{
....
if (_httpClient == null)
{
_httpClient = new HttpClient();
}
var response = _httpClient.GetAsync("user/1);
}
public void DoPost(foo Foo)
{
if (_httpClient == null)
{
_httpClient = new HttpClient();
}
var formData = new Dictionary<string, string>
{
{"Name", "Joe smith"},
{"Age", "40"}
};
var response = _httpClient.PostAsync("user",
new FormUrlEncodedContent(formData));
}
Run Code Online (Sandbox Code Playgroud)
所以我不知道如何使用FakeItEasy
,伪造出HttpClient GetAsync
和PostAsync
方法.
生产代码不会在HttpClient中传递,但单元测试将传递由FakeItEasy制作的假实例.
例如.
[Fact]
public void GivenBlah_DoGet_DoesSomething()
{ …
Run Code Online (Sandbox Code Playgroud) 使用FakeItEasy,如何检查我的对象的方法是否在同一个对象上调用另一个方法?
考试:
[TestMethod]
public void EatBanana_CallsWillEat()
{
var banana = new Banana();
var myMonkey = new Monkey();
myMonkey.EatBanana(banana);
//this throws an ArgumentException, because myMonkey is a real instance, not a fake
A.CallTo(() => myMonkey.WillEat(banana)
.MustHaveHappened();
}
Run Code Online (Sandbox Code Playgroud)
班级:
public class MyMonkey {
private readonly IMonkeyRepo _monkeyRepo;
public MyMonkey(IMonkeyRepo monkeyRepo) {
_monkeyRepo = monkeyRepo;
}
public void EatBanana(Banana banana) {
//make sure the monkey will eat the banana
if (!this.WillEat(banana)) {
return;
}
//do things here
}
public bool WillEat(Banana banana) { …
Run Code Online (Sandbox Code Playgroud) 我们正在考虑使用FakeItEasy作为我们的模拟框架.
为了在FakeItEasy上为团队举办研讨会,我正在寻找一个很好的教程,详细解释了这个框架.我试着查看github上的文档,但发现它非常简洁.
如果你知道一个精美的页面/视频,请你指点一下.谢谢.
我有一个使用 FakeitEasy 伪造的服务,我正在尝试调用它的方法。这是代码
var client = container.Resolve<MyService>();
A.CallTo(() => client.GetUserProfile(userName)).Returns(null);
Run Code Online (Sandbox Code Playgroud)
GetUserProfile 方法在实际实现中返回一些对象。但出于某种原因,我希望此方法返回空值。我正在使用上面的代码来实现这个目的,但它返回的是 Fake 对象而不是 null。
这是我正在使用的测试设置
[Test]
public void MyTest(string sitecollectionGuid, string customerName)
{
var mockHttpContext = SetupHttpContext(sitecollectionGuid, customerName);
var client = container.Resolve<MyService>();
A.CallTo(() => client.GetUserProfile(userName)).Returns(null);
var controllerContext = new ControllerContext(mockHttpContext, new RouteData(), A.Fake<ControllerBase>());
controller.ControllerContext = controllerContext;
var result = controller.CheckUsername(userName);
Assert.IsNotNull(result, "Result is not as expected");
}
Run Code Online (Sandbox Code Playgroud)
制作方法如下
public UserDAO GetUserProfile(string userName)
{
UserDAO objUserProfile = new UserDAO();
IUsers objUsers = (IUsers)Global.Container["Users"];
IUser objUser = objUsers.GetByUserName(userName);
if (objUser == …
Run Code Online (Sandbox Code Playgroud) 使用NUnit 2.6.4和FakeItEasy 1.25.2对Visual Studio 2013 Community Edition中的C#代码进行单元测试
以下测试片段按预期执行
[Test]
public void test_whatIsUpWithStreamRead()
{
Stream fakeStream = A.Fake<Stream>();
byte[] buffer = new byte[16];
int numBytesRead = fakeStream.Read(buffer, 0, 16);
Assert.AreEqual(0, numBytesRead);
}
Run Code Online (Sandbox Code Playgroud)
但是只要我用CallTo/Returns()或ReturnsLazily()语句装饰我的假货......
[Test]
public void test_whatIsUpWithStreamRead()
{
Stream fakeStream = A.Fake<Stream>();
A.CallTo(() => fakeStream.Read(A<byte[]>.Ignored, A<int>.Ignored, A<int>.Ignored)).Returns(1);
byte[] buffer = new byte[16];
int numBytesRead = fakeStream.Read(buffer, 0, 16);
Assert.AreEqual(1, numBytesRead);
}
Run Code Online (Sandbox Code Playgroud)
fakeStream.Read()
抛出System.InvalidOperationException并显示以下消息:
"指定的out和ref参数的值的数量与调用中的out和ref参数的数量不匹配."
从内部FakeItEasy.Configuration.BuildableCallRule.ApplyOutAndRefParametersValueProducer(IInterceptedFakeObjectCall fakeObjectCall)
,这对我来说似乎很奇怪,因为Stream.Read()
没有任何out/ref参数.
这是我应该在https://github.com/FakeItEasy上报告的错误,还是我错过了什么?
谢谢
根据此处的 FakeItEasy 教程,WithArgumentsForConstructor()
扩展方法不会调用类构造函数:
// Specifying arguments for constructor using expression. This is refactoring friendly!
// The constructor seen here is never actually invoked. It is an expression and it's purpose
// is purely to communicate the constructor arguments which will be extracted from it
var foo = A.Fake<FooClass>(x => x.WithArgumentsForConstructor(() => new FooClass("foo", "bar")));
Run Code Online (Sandbox Code Playgroud)
但是,我的Person
类构造函数中的断点在下面的测试中被触发。为何如此?我使用WithArgumentsForConstructor()
不正确吗?
[Test]
public void Constructor_With_Arguments()
{
var driver = A.Fake<Person>(x => x.WithArgumentsForConstructor(() => new Person("Jane", 42)));
var age …
Run Code Online (Sandbox Code Playgroud) 我正在使用FakeItEasy伪造一些实体框架调用,以确保正确映射一堆奇怪的遗留数据库表.
我需要断言,将具有与特定DeliveryAddress匹配的发票的客户添加到数据库中.
如果我这样做:
A.CallTo(() => db.Customers.Add(
A<Customer>.That.Matches(
c => c.Invoices.First().Address == EXPECTED_ADDRESS)
)
)).MustHaveHappened();
Run Code Online (Sandbox Code Playgroud)
代码完美无缺.我希望通过在其他地方移动期望来简化语法,但是当我这样做时:
var expected = A<Customer>.That.Matches(
c => c.Invoices.First().Address == EXPECTED_ADDRESS)
);
A.CallTo(() => db.Customers.Add(expected)).MustHaveHappened();
Run Code Online (Sandbox Code Playgroud)
测试失败.FakeItEasy代码中发生了什么,这意味着期望表达式在内联时有效但无法在变量中捕获并在以后重用?
我正在使用 C#,我需要测试接受 System.Net.Http.Headers.HttpRequestHeaders 作为参数的方法之一。我们使用 FakeItEasy 进行测试。
但似乎 HttpResponseHeaders - 没有构造器(并且它是密封的),并且它使用 HttpHeader 作为基础。HttpHeader - 有构造函数,但 Header 属性只允许 get。
有没有办法构建虚拟/假的 HttpResponseHeaders 或 HttpResponseMessage 并在其中预设所需的标头值?
我最近试图使用FakeItEasy但是我无法在没有解决许多怪癖的情况下从具体类创建假.
我尝试了以下方法:
public class MyObject {
public MyObject(){}
}
...
MyObject fakeObject = A.Fake<MyObject>();
Run Code Online (Sandbox Code Playgroud)
这导致了一个没有匹配争议异常的构造函数
接下来我尝试了:
public class MyObject {
public MyObject(string temp){}
}
...
MyObject fakeObject = A.Fake<MyObject>(x => x.WithArgumentsForConstructor(() => new MyObject("temp")));
Run Code Online (Sandbox Code Playgroud)
这导致了类似的错误.
最后我试过:
public class MyObject {
//public MyObject(){}
}
...
MyObject fakeObject = A.Fake<MyObject>();
Run Code Online (Sandbox Code Playgroud)
这让我最终创造了假货.我很困惑为什么大多数伪造一个具体类的例子都提到这个,这比我发现它更容易?为什么使用上面的试验#2记录的方法不起作用?
伪造一个没有记录的具体类是否有一些限制?
fakeiteasy ×10
c# ×7
.net ×4
unit-testing ×4
mocking ×3
c#-4.0 ×1
expression ×1
http-headers ×1