Tag: mocking

我可以在Rhino-Mocks 3.6中使用AAA语法测试方法调用顺序吗?

如果Method1在Rhino-mocks 3.6中调用1st,然后调用After后面的Method2,然后使用AAA语法调用Method3,是否可以测试以下示例? // Assert var mock = MockRepository.GenerateMock(); // Act myObject.Service = mock; // How should I change this part to ensure that Rhino Mocks check the call order as well? mock.AssertWasCalled(m=>m.Method1()); mock.AssertWasCalled(m=>m.Method2()); mock.AssertWasCalled(m=>m.Method3());

TDD和Mocking TcpClient

人们如何处理模拟TcpClient(或TcpClient之类的东西)? 我有一个接收TcpClient的服务。 我应该把它包装成更可模仿的东西吗? 我该怎么办呢?

模拟AsNoTrackingentity framework

我如何模拟AsNoTracking方法? 在下面的示例中,DbContext已注入服务类。如果我从GetOrderedProducts方法中删除AsNoTracking扩展方法,但是AsNoTracking测试失败,因为它返回null,它工作正常。 我也尝试过模拟AsNoTracking来返回正确的值,但它没有用。 public interface IUnitOfWork { IDbSet Set() where TEntity : class; int SaveAllChanges(); } public class Entites : DbContext, IUnitOfWork { public virtual DbSet Products { get; set; } // This is virtual because Moq needs to override the behaviour public new virtual IDbSet Set() where TEntity : class // This is virtual […]

如何实施Moles隔离框架?

Moles是Microsoft创建的隔离框架。 Moles的一个很酷的function是它可以“模拟”静态/非虚拟方法和密封类(这对于像Moq这样的框架是不可能的)。 以下是Moles可以做的快速演示: Assert.AreNotEqual(new DateTime(2012, 1, 1), DateTime.Now); // MDateTime is part of Moles; the below will “override” DateTime.Now’s behavior MDateTime.NowGet = () => new DateTime(2012, 1, 1); Assert.AreEqual(new DateTime(2012, 1, 1), DateTime.Now); 看起来像Moles能够在运行时修改DateTime.Now之类的CIL主体。 由于Moles不是开源的,我很想知道Moles使用哪种机制来在运行时修改方法的CIL。 谁能摆脱任何光明?

模拟validation()调用

我正在进行unit testing以查看是否调用了一个方法。 [Fact] public void Can_Save_Project_Changes() { //Arrange var user = new AppUser() { UserName = “JohnDoe”, Id = “1” }; Mock mockRepo = new Mock(); Mock<UserManager> userMgr = GetMockUserManager(); userMgr.Setup(x => x.FindByNameAsync(It.IsAny())).ReturnsAsync(new AppUser() { UserName = “JohnDoe”, Id = “1” }); var contextUser = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.UserName), new Claim(ClaimTypes.NameIdentifier, […]