标签: fakeiteasy

怎么样.IsAny <T>在FakeItEasy?

有谁知道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();

moq fakeiteasy

5
推荐指数
1
解决办法
1556
查看次数

在同一个模拟实例上断言对公共方法的调用

我有以下测试

[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)

问题是,该呼叫DealDamageAttack方法没有被注册,因为该方法中,thisrealAttacker不是wrappedAttacker攻击者因此调用该方法不被拦截.

我该如何测试这个断言?可以用FakeItEasy完成吗?是否有一个不同的模拟框架允许我测试它?

c# nunit unit-testing mocking fakeiteasy

5
推荐指数
1
解决办法
1017
查看次数

FakeItEasy是否支持Partial Mocks的想法?

类似于关于NSubstitute的这个问题,我想知道是否能够使用FakeItEasy库实现部分模拟.

FakeItEasy似乎比moq具有更好的语法(就像前者处理将参数传递给伪类的构造函数的强类型方式).我正在考虑转换到FakeItEasy,但我真的需要部分模拟支持.

c# unit-testing mocking fakeiteasy

5
推荐指数
1
解决办法
1306
查看次数

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)

c# unit-testing mocking fakeiteasy

5
推荐指数
1
解决办法
1722
查看次数

使用F#和FakeItEasy伪造返回值

我试图使用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可能是一种更容易的方法.

f# unit-testing fakeiteasy

5
推荐指数
1
解决办法
140
查看次数

使用私有构造函数和静态创建方法以及私有 id 属性模拟类

我正在尝试使用 AutoFixture 来简化我的生活。

我如何模拟一个班级

  • 非公共构造函数和
  • 非公共 ID 和
  • 静态“创建”方法?

例子

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)

c# unit-testing mocking autofixture fakeiteasy

4
推荐指数
1
解决办法
1060
查看次数

非虚拟方法无法截获

我是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)

unit-testing mocking fakeiteasy

4
推荐指数
1
解决办法
3507
查看次数

.NET CORE 测试 - 使用 FakeItEasy 模拟 IHttpContextAccessor

我一直在为一些 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)

.net c# integration-testing fakeiteasy asp.net-core

4
推荐指数
2
解决办法
6937
查看次数

FakeItEasy 配置 fake 以在下次调用时抛出异常并返回值

我们必须实施重试机制。

为了测试RetryProvider,我想要一个假的类在前两次调用时抛出异常,但在第三次调用时返回一个有效的对象。

在正常情况下(不抛出异常)我们可以使用 A.CallTo(() => this.fakeRepo.Get(1)).ReturnsNextFromSequence("a", "b", "c");

我想要类似的东西:

  • 第一次调用: throw new Exception();
  • 第二次调用: throw new Exception();
  • 第三次调用:返回“成功”;

我如何配置我的假货来做到这一点?

提前致谢

.net c# unit-testing mocking fakeiteasy

4
推荐指数
2
解决办法
1694
查看次数

为什么 .NET (Core) 中的大多数模拟框架不能模拟静态和私有方法?

我的问题是关于此限制的技术原因,而不是关于如何修复它。

为什么像Telerik JustMockTypemock Isolator这样的框架支持这些功能,但我们不能在MoqFakeItEasyNSubstitute等中拥有这些功能?

单元测试中提到的项目是否不必要?

c# moq mocking nsubstitute fakeiteasy

4
推荐指数
1
解决办法
1052
查看次数