moq

How to moq a static class with a static method (UnitOfWork case)?

末鹿安然 提交于 2019-12-04 10:53:49
问题 I have these classes: public static class UnitOfWorkSS { public static IUnitOfWork Begin() { return IoC.Resolve<IUnitOfWork>(); } } public class PostService { using (IUnitOfWork unitOfWork = UnitOfWorkSS.Begin()) { //don't forget to sanitize html content htmlContent = _htmlSanitizer.Sanitize(htmlContent); IPost post = _factory.CreatePost(byUser, title, htmlContent); _postRepository.Add(post); unitOfWork.Commit(); } } How can I mock the classes UnitOfWorkSS and unitOfWork ? 回答1: It looks like

Moq with Task await

主宰稳场 提交于 2019-12-04 08:14:38
问题 Since I have converted my WCF methods to Async, my unit tests have failed, and I can't figure out the correct syntax to get them to work. Cllient proxy class public interface IClientProxy { Task DoSomething(CredentialDataList credentialData, string store); } service class public class CredentialSync : ICredentialSync { private ICredentialRepository _repository; private IClientProxy _client; public CredentialSync() { this._repository = new CredentialRepository(); this._client = new ClientProxy

Mocking VB.NET Methods With Moq

跟風遠走 提交于 2019-12-04 07:17:36
I am trying to unit test a controller action that uses the membership provider to update user details. I am using Moq which so far has been easy to use. The problem is I can't seem to get it to mock calls to methods that don't return anything. <TestMethod()> _ Public Sub Can_Update_User() ' Arrange _membershipService.Setup(Function(x) x.UpdateUser(It.IsAny(Of MembershipUser))) Dim controller As New UsersController(_membershipService.Object, _roleProvider.Object, _supportWorksService.Object, _portalClientService.Object) ' Act Dim result As ViewResult = controller.Edit("testUser", New

how do you mock an xml for unit testing?

和自甴很熟 提交于 2019-12-04 06:38:00
I need to unit testing this GetData method. public MessageResponse GetData(XmlElement requestElement) { MessageResponse MsgResponse = new MessageResponse(); if (requestElement.Attributes["employeeNo"] == null){ MsgResponse.Messages = new List<string>(); MsgResponse.Messages.Add("Attribute employeeNo is missing"); MsgResponse.Error = true; return MsgResponse; } if (requestElement.Attributes["xmlEmployeeName"] == null){ MsgResponse.Messages.Add("Attribute xmlEmployeeName is missing"); MsgResponse.Error = true; return MsgResponse; } return MsgResponse; } this method needs a XmlElement parameter.

MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action

时间秒杀一切 提交于 2019-12-04 04:29:26
问题 Here's my problem - I am using MvcMailer to create nicely formatted emails using Razor syntax, and it's a great tool to use for that! The issue I'm running into is this - here's some syntax from my View for one of the emails i send: <p>Click here to return to <a href="@Url.Abs(Url.Action("Details", "Home", new{ Id=ViewBag.IdeaId}))">@ViewBag.IdeaName</a></p> Whenever I try to run my unit tests, I get the following error message: Can we send out email notifications for new comments?: System

Verify that either one method or the other was invoked in a unit test

99封情书 提交于 2019-12-04 04:22:29
问题 Example: public bool Save(MyObj instance) { if (instance.IsNew) { this.repository.Create(instance); } else { this.repository.Update(instance); } } How do I create a test in Moq that verifies: that a property IsNew is being read that either Create() or Update() has been invoked 回答1: Off the top of my head: Verifying that the IsNew property is being read: var mock = new Mock<MyObj>(); mock.Setup(m => m.IsNew).Returns(true).Verifiable(); //... sut.Save(mock.Object); //... mock.Verify(); In the

Moq & C#: Invalid callback. Setup on method with parameters cannot invoke callback with parameters

混江龙づ霸主 提交于 2019-12-04 03:52:08
问题 The actual interface signature goes like this Task<GeneralResponseType> UpdateAsync(ICustomerRequest<IEnumerable<CustomerPreference>> request, CancellationToken cancellationToken, ILoggingContext loggingContext = null); Testcase: ICustomerRequest<IEnumerable<CustomerPreference>> t = null; CancellationToken t1 = new CancellationToken(); LoggingContext t2 = null; this.customerPreferenceRepositoryMock.Setup(x => x.UpdateAsync( It.IsAny<ICustomerRequest<IEnumerable<CustomerPreference>>>(), It

How to moq Entity Framework SaveChangesAsync?

醉酒当歌 提交于 2019-12-04 03:19:01
Mock<IDbContext> dbContext; [TestFixtureSetUp] public void SetupDbContext() { dbContext = new Mock<IDbContext>(); dbContext.Setup(c => c.SaveChanges()).Verifiable(); dbContext.Setup(c => c.SaveChangesAsync()).Verifiable(); dbContext.Setup(c => c.Customers.Add(It.IsAny<Customer>())) .Returns(It.IsAny<Customer>()).Verifiable(); } [Test] public async Task AddCustomerAsync() { //Arrange var repository = new EntityFrameworkRepository(dbContext.Object); var customer = new Customer() { FirstName = "Larry", LastName = "Hughes" }; //Act await repository.AddCustomerAsync(customer); //Assert dbContext

Does Moq.Mock.Verify() compare parameters using identity or .Equals()?

心不动则不痛 提交于 2019-12-04 03:08:26
In a command like var mockObj = new Mock<MyObject>() var anotherObj = Utilities.DoStuff(); // some tests... mockObj.Verify(foo => foo.someMethod(anotherObj)); Does Moq use comparison by identity or by using .Equals() to determine whether someMethod() was ever called with anotherObj as a parameter? In other words, does the object I indicate as a parameter for foo.someMethod() have to be the exact same object that someMethod() was called with earlier for the verification to pass, or does it only have to be one that is equal to anotherObj ? Moq will compare by identity, it will be looking for the

Is the moq project dead? Is it wise for me to invest in learning it? [closed]

核能气质少年 提交于 2019-12-04 02:47:17
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I am fairly new to mocking frameworks and was trying to decide which one will be a good bet to start working on. I have been looking at this question about the best mocking framework, and I can see a lot of people preferring moq, but when i saw the moq project's change list, i can see that it has not been