有谁知道It.IsAny<T>
FakeItEasy 中Moq的等价物?
我想断言用一个Type实例调用一个方法
v.Do(new Foo());
Run Code Online (Sandbox Code Playgroud)
我试过了:
A.CallTo(() => v.Do(A<Foo>.Ignored)).MustHaveHappened();
Run Code Online (Sandbox Code Playgroud)
但这也接受v.Do()
;
我有以下测试
[Test]
public void Attack_TargetWith3Damage_CausesAttackerToDeal3DamageToTarget()
{
var realAttacker = CreateCreature(damage: 3);
var wrappedAttacker = A.Fake<ICreature>(x => x.Wrapping(realAttacker));
var target = A.Fake<ICreature>();
wrappedAttacker.Attack(target);
A.CallTo(() => wrappedAttacker.DealDamage(target, 3)).MustHaveHappened();
}
Run Code Online (Sandbox Code Playgroud)
问题是,该呼叫DealDamage
从Attack
方法没有被注册,因为该方法中,this
是realAttacker
不是wrappedAttacker
攻击者因此调用该方法不被拦截.
我该如何测试这个断言?可以用FakeItEasy完成吗?是否有一个不同的模拟框架允许我测试它?
类似于关于NSubstitute的这个问题,我想知道是否能够使用FakeItEasy库实现部分模拟.
FakeItEasy似乎比moq具有更好的语法(就像前者处理将参数传递给伪类的构造函数的强类型方式).我正在考虑转换到FakeItEasy,但我真的需要部分模拟支持.
我想重置虚假呼叫历史记录或忽略呼叫.
在测试类构造函数的Init方法中调用伪断言方法,我想忽略此调用,因为它不是测试操作的一部分.
这是一个例子:
[TestClass]
public class UnitTest1
{
private MyFakedClass myFakedObject;
private SUT sut;
[TestInitialize]
public void Init()
{
myFakedObject = A.Fake<MyFakedClass>();
sut = new SUT(myFakedObject); //constructor calls myFakedObject.AssertedMethod()
}
[TestMethod]
public void TestMethod1()
{
sut.TestedMethod(); //TestedMethod calls myFakedObject.AssertedMethod() again...
A.CallTo(() => myFakedObject.AssertedMethod()).MustHaveHappened(Repeated.Exactly.Once);
//...So this is false
}
}
Run Code Online (Sandbox Code Playgroud) 我试图使用FakeItEasy来模拟在C#中定义的接口
public interface IMyInterface
{
int HeartbeatInterval { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在F#测试我做
let myFake = A.Fake<IMyInterface>()
A.CallTo(fun () -> ((!myFake).HeartbeatInterval)).Returns(10) |> ignore
Run Code Online (Sandbox Code Playgroud)
在测试运行器中运行此结果
System.ArgumentException
Expression of type 'Microsoft.FSharp.Core.FSharpFunc`2[Microsoft.FSharp.Core.Unit,System.Int32]' cannot be used for return type 'System.Int32'
事实上,它似乎是为任何返回类型执行此操作,例如,如果HeartbeatInterval返回类型,foo
则抛出的异常将是类型foo
而不是System.Int32
.
我做错了还是F#和FakeItEasy之间有些不兼容?
我已经想到使用Object Expression可能是一种更容易的方法.
我正在尝试使用 AutoFixture 来简化我的生活。
我如何模拟一个班级
例子
public class User
{
private User(){}
/// <summary>
/// Created by Database...
/// </summary>
public long? Id { get; protected set; }
public string Name { get; protected set; }
public static User Create(string name)
{
var user = new User {Name = name};
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
我试过使用 Factory 和 SpecimenBuilder 的组合:
[Fact]
public void CreatingUserWithId_Should_Work_UsingFactoryAndSpecimenBuilder()
{
IFixture fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
fixture.Customizations.Add(new UserBuilder());
fixture.Customize<User>(o => o.FromFactory(() => …
Run Code Online (Sandbox Code Playgroud) 我是FakeItEasy的新手并尝试解决问题.
我上课了
public class Events
{
public List<Events> SaveEvents()
{
// Call to repository and return 1(success) or -1(fail)
//If the repository return 1 then need to make another call to save the action in db
//Sample Code here
AuditLogService log = new AuditLogService();
log.CallLog();
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试代码:
[TestMethod]
public void EventValidation()
{
//Arrange
var Fakeevents = A.Fake<Events>();
var log = A.Fake<AuditLogService>();
var _EventsController = new EventsController(Fakeevents);
_EventsController.SaveEvents();
A.CallTo(
() => Fakeevents.SaveEvents().Retunr(1).AssignsOutAndRefParameters(status)
A.CallTo(
() => log.CallLog()).MustHaveHappened(Repeated.AtLeast.Once);
}
I am getting …
Run Code Online (Sandbox Code Playgroud) 我一直在为一些 web api 集成测试模拟 IHttpContextAccessor。我的目标是能够模拟 IHttpContextAccessor 并返回 NameIdentifier 声明和 RemoteIpAddress。
测试
public class InsertUser : TestBase
{
private UserController _userController;
[OneTimeSetUp]
public void OneTimeSetUp()
{
IStringLocalizer<UserController> localizer = A.Fake<IStringLocalizer<UserController>>();
_userController = new UserController(localizer, Mapper, UserService, StatusService, IdentityService);
_userController.ControllerContext = A.Fake<ControllerContext>();
_userController.ControllerContext.HttpContext = A.Fake<DefaultHttpContext>();
var fakeClaim = A.Fake<Claim>(x => x.WithArgumentsForConstructor(() => new Claim(ClaimTypes.NameIdentifier, "1")));
var fakeIdentity = A.Fake<ClaimsPrincipal>();
A.CallTo(() => fakeIdentity.FindFirst(ClaimTypes.NameIdentifier)).Returns(fakeClaim);
A.CallTo(() => _userController.ControllerContext.HttpContext.User).Returns(fakeIdentity);
StatusTypeEntity statusType = ObjectMother.InsertStatusType(StatusTypeEnum.StatusType.User);
StatusEntity status = ObjectMother.InsertStatus(StatusEnum.Status.Active, statusType);
ObjectMother.InsertUser("FirstName", "LastName", "Email@Email.Email", "PasswordHash", "PasswordSalt", status);
}
public …
Run Code Online (Sandbox Code Playgroud) 我们必须实施重试机制。
为了测试RetryProvider
,我想要一个假的类在前两次调用时抛出异常,但在第三次调用时返回一个有效的对象。
在正常情况下(不抛出异常)我们可以使用 A.CallTo(() => this.fakeRepo.Get(1)).ReturnsNextFromSequence("a", "b", "c");
我想要类似的东西:
我如何配置我的假货来做到这一点?
提前致谢
我的问题是关于此限制的技术原因,而不是关于如何修复它。
为什么像Telerik JustMock和Typemock Isolator这样的框架支持这些功能,但我们不能在Moq或FakeItEasy或NSubstitute等中拥有这些功能?
单元测试中提到的项目是否不必要?
fakeiteasy ×10
c# ×7
mocking ×7
unit-testing ×7
.net ×2
moq ×2
asp.net-core ×1
autofixture ×1
f# ×1
nsubstitute ×1
nunit ×1