moq

Need Help understanding this code

感情迁移 提交于 2019-12-06 14:18:46
I am trying to learn unit testing. I am trying to unit test some Memembership stuff I am making in asp.net mvc 1.0. I been following a book on MVC and I am confused about some stuff that hopefully someone can clear up for me. I am using Nunit and Moq for my frameworks. Question 1: public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider provider) { FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); Provider = provider ?? Membership.Provider; } I am kinda confused what "??" does I never really seen it before. Like I don't even know whats happening really in

Mocking of extension method result in System.NotSupportedException

本秂侑毒 提交于 2019-12-06 13:41:45
问题 I'm unit testing a ClientService that uses the IMemoryCache interface: ClientService.cs: public string Foo() { //... code _memoryCache.Set("MyKey", "SomeValue", new TimeSpan(0, 0, 60)); } When I try to mock the IMemoryCache 's Set extension with: AutoMock mock = AutoMock.GetLoose(); var memoryCacheMock = _mock.Mock<IMemoryCache>(); string value = string.Empty; // Attempt #1: memoryCacheMock .Setup(x => x.Set<string>(It.IsAny<object>(), It.IsAny<string>(), It.IsAny<TimeSpan>())) .Returns("");

Unit Testing Unit of Work and Generic Repository Pattern framework using Moq

会有一股神秘感。 提交于 2019-12-06 13:32:21
问题 I am unit testing a Service which is using the a Unit of Work and Generic Repository using Moq. The problem is that in the service class the _subsiteRepository is always null when I run the test in debug mode. The setup of the Service Class I am mocking private readonly IRepository<Subsite> _subsiteRepository; public PlatformService(IUnitOfWork<PlatformContext> unitOfWork) { _subsiteRepository = unitOfWork.GetRepository<Subsite>(); } and the method in this class that am testing. The problem

How can I stub an interface method using Moq

…衆ロ難τιáo~ 提交于 2019-12-06 11:58:42
Is there a way to stub out a method using Moq? I saw quite a few questions with similar titles, but none that actually answered my question. Here was a unit testing example I was given and I found it very difficult to test using Moq. What I would like to do is unit test the EmailTasks.UserIsValidForNotice() method: public class User { public DateTime JoinDate { get; set; } public bool Active { get; set; } } public class EmailTasks { IUserRepository repo; public EmailTasks(IUserRepository repo) { this.repo = repo; } public IList<User> UserIsValidForNotice(DateTime minDate) { return repo.FindAll

Using Moq's VerifySet in VB.NET

谁说我不能喝 提交于 2019-12-06 11:37:20
I have a function that updates a user in the asp.net membership provider. <AcceptVerbs(HttpVerbs.Post)> Public Function EnableUser(ByVal id As String) As JsonResult Dim usr As StargatePortalUser = _membershipService.GetUser(id, Nothing) usr.IsApproved = True _membershipService.UpdateUser(usr) Dim response As New AjaxResponse(usr.UserName) Return Json(response) End Function I am trying to test this function to ensure the IsApproved property is set correctly <TestMethod()> Public Sub Service_Can_Enable_A_User_Account() ' Arrange Dim usr As New Mock(Of MembershipUser) usr.SetupProperty(Function(u

Unit test WebApi2 passing header values

萝らか妹 提交于 2019-12-06 10:53:38
问题 I am working on a project using WebApi2. With my test project I am using Moq and XUnit. So far testing an api has been pretty straight forward to do a GET like [Fact()] public void GetCustomer() { var id = 2; _customerMock.Setup(c => c.FindSingle(id)) .Returns(FakeCustomers() .Single(cust => cust.Id == id)); var result = new CustomersController(_customerMock.Object).Get(id); var negotiatedResult = result as OkContentActionResult<Customer>; Assert.NotNull(negotiatedResult); Assert.IsType

Why this mock with Expression<Func<T,bool> is not matching?

橙三吉。 提交于 2019-12-06 10:41:28
i am new on mock and i am trying to do this mock example : Repository.cs public class Repository : IRepository { public List<Person> GetForExpression(Expression<Func<Person,bool>> expression ) { ... //get person from orm using expression } } PersonService.cs public class PersonService { private IRepository _repository; public PersonService(IRepository repository) { _repository = repository; } public List<Person> GetAllPersonsWith18More() { var result = _repository.GetForExpression(x => x.Age > 18); return result; } } Test: [TestClass] public class UnitTest1 { [TestMethod] public void

Moq unit test with It.IsAny<DateTime>() fails

北战南征 提交于 2019-12-06 10:29:37
I'm using Moq to write the unit tests for a project, and one of the tests is failing when I try to verify that a DateTime property is assigned a value. Here's my Verify (which fails): _mockTaskContext.Verify(context => context.TaskQueue.AddObject(It.Is<TaskQueueItem>( task_queue => task_queue.TaskCode == (int)TaskCode.MyTask && task_queue.ClientID == ExpectedClientID && task_queue.JobNumber == It.IsAny<int>() && task_queue.Requester == String.Empty && task_queue.JobStatus == (int)JobStatus.Submitted && task_queue.TimeQueued == It.IsAny<DateTime>() && task_queue.TimeStarted == new DateTime(1900

System.NotSupportedException: Unsupported expression: p => (p.UserProfileId == 1)

删除回忆录丶 提交于 2019-12-06 10:16:27
I have a test that uses System.Func expressions. It should be pretty straight forward, but the test keeps on failing. Test: [TestMethod] public void GetUser() { var username = "john@resilientplc.com"; var user = new User() { UserId = 1, Username = username, Password = "123456789" }; someDataMock.Setup(s => s.GetUser(p => p.UserId == 1)).Returns(user); var result = userProfileModel.GetUser(user.UserId); Assert.AreEqual(user, result); } Implementation UserProfileModel : public User GetUser(long userId) { return someDataMock.GetUser(u => u.UserId == UserId); } Error: System.NotSupportedException:

Mocking the “Properties” property of DirectoryEntry

末鹿安然 提交于 2019-12-06 09:23:39
问题 I'm trying to unit test some Active Directory code, pretty much the same as outlined in this question: Create an instance of DirectoryEntry for use in test The accepted answer suggests implementing a wrapper/adapter for the DirectoryEntry class, which I have: public interface IDirectoryEntry : IDisposable { PropertyCollection Properties { get; } } public class DirectoryEntryWrapper : DirectoryEntry, IDirectoryEntry { } The problem is that the " Properties " property on my IDirectoryEntry mock