moq

When using Moq Verify() method invocation count, have failing test's error message contain actual method invocation count using Moq

此生再无相见时 提交于 2019-12-01 14:32:29
问题 Consider the following, where I am testing that an injected dependency's method is called a specific number of times: [Fact] public void WhenBossTalksEmployeeBlinksTwice() { // arrange var employee = new Mock<IEmployee>(); employee.Setup(e => e.Blink()); var boss = new Boss(employee.Object); // act boss.Talk(); // assert employee.Verify(e => e.Blink(), Times.Exactly(2)); // Passes as expected employee.Verify(e => e.Blink(), Times.Exactly(1)); // Fails as expected } When I force the failing

Mocking an IoC Container?

二次信任 提交于 2019-12-01 10:52:15
Does it make sense to mock an IoC container? If so how would I go about it, using Moq? I am creating a Prism 4 app, using Unity 2.0 as the IoC container. I inject the container into classes that need its services, rather than using Prism's ServiceLocator . For unit testing, unless I need other Prism services for my test, I simply instantiate the container and register mocks with it. I pass the container to the class under test, which resolves the mocks. It's all fairly simple, but I am wondering if I should mock the container? Why? If so, how would I do that, if I am using Moq as my mocking

How to use setup of a mocked anonymous type?

你。 提交于 2019-12-01 09:02:09
I have the following repository: interface IReportingRepository where T: Report { IEnumerable<T> GetReports<T>(object constraints); } and I am trying to mock out a call to this repository as: var reportingRepostory = new Mock<IReportingRepository>(); reportingRepostory.Setup(x => x.GetReports<ServiceReport (Moq.It.IsAny<object>())). Returns(new List<ServiceReport>(){Report1, Report2}); However instead of passing in Moq.It.IsAny<object>() I want to pass the anonymous type new {Activated = true, Enabled = true} so that I can setup my expectation that the correct anonymous type is used. You can

Mock CloudBlobClient with AutoFac and AutoMock

為{幸葍}努か 提交于 2019-12-01 08:28:50
I'm trying to write unit tests for my AzureBlobRepository. The repository receives a CloubBlobClient in the constructor. I want to mock the client, but this gives an exception: using (var mock = AutoMock.GetLoose()) { var mockClient = mock.Mock<CloudBlobClient>(); } Cannot choose between multiple constructors with equal length 2 on type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component is registered. Ofcourse, in my unit test I am not registering anything, so that message is not very

Unit Testing Custom Password Validators in ASP.NET Core

跟風遠走 提交于 2019-12-01 07:39:19
I have a CustomPasswordValidator.cs file that overrides PasswordValidator public class CustomPasswordValidator : PasswordValidator<AppUser> { //override the PasswordValidator functionality with the custom definitions public override async Task<IdentityResult> ValidateAsync(UserManager<AppUser> manager, AppUser user, string password) { IdentityResult result = await base.ValidateAsync(manager, user, password); List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result.Errors.ToList(); //check that the username is not in the password if (password.ToLower().Contains(user

Mock CloudBlobClient with AutoFac and AutoMock

余生颓废 提交于 2019-12-01 07:30:18
问题 I'm trying to write unit tests for my AzureBlobRepository. The repository receives a CloubBlobClient in the constructor. I want to mock the client, but this gives an exception: using (var mock = AutoMock.GetLoose()) { var mockClient = mock.Mock<CloudBlobClient>(); } Cannot choose between multiple constructors with equal length 2 on type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient'. Select the constructor explicitly, with the UsingConstructor() configuration method, when the component

How to use Moq to satisfy a MEF import dependency for unit testing?

ぃ、小莉子 提交于 2019-12-01 06:34:14
This is my interface public interface IWork { string GetIdentifierForItem(Information information); } and my class public class A : IWork { [ImportMany] public IEnumerable<Lazy<IWindowType, IWindowTypeInfo>> WindowTypes { get; set; } public string GetIdentifierForItem(Information information) { string identifier = null; string name = information.TargetName; // Iterating through the Windowtypes // searching the 'Name' and then return its ID foreach (var windowType in WindowTypes) { if (name == windowType.Metadata.Name) { identifier = windowType.Metadata.UniqueID; break; } } return identifier; }

Unit Test for a View Attribute using Moq

一个人想着一个人 提交于 2019-12-01 06:28:06
I am using Moq for unit testing and I would like to test for a view's attribute. In this case the Authorize attribute. Example View Code: [Authorize(Roles = "UserAdmin")] public virtual ActionResult AddUser() { // view logic here return View(); } So I would like to test the view attribute when I act on this view with a user that is in the role of UserAdmin and a user that is not in the role of user admin. Is there anyway to do this ? Example Test: [Test] public void Index_IsInRole_Customer() { // Arrange UserAdminController controller = _controller; rolesService.Setup(r => r.IsUserInRole(It

Unit Testing Custom Password Validators in ASP.NET Core

冷暖自知 提交于 2019-12-01 06:12:08
问题 I have a CustomPasswordValidator.cs file that overrides PasswordValidator public class CustomPasswordValidator : PasswordValidator<AppUser> { //override the PasswordValidator functionality with the custom definitions public override async Task<IdentityResult> ValidateAsync(UserManager<AppUser> manager, AppUser user, string password) { IdentityResult result = await base.ValidateAsync(manager, user, password); List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result

Moq Mocking and tracking Session values

主宰稳场 提交于 2019-12-01 06:07:21
I'm having problems returning a Session value set from mocking using Moq. Using the following public class TestHelpers { public long sessionValue = -1; public HttpContextBase FakeHttpContext() { var httpContext = new Mock<HttpContextBase>(); var session = new Mock<HttpSessionStateBase>(); httpContext.Setup(x => x.Session).Returns(session.Object); httpContext.SetupGet(x => x.Session["id"]).Returns(sessionValue); httpContext.SetupSet(x => x.Session["id"] = It.IsAny<long>()) .Callback((string name, object val) => { sessionValue = (long)val; }); } } When I try to obtain the value outside using var