How to add claims in a mock ClaimsPrincipal

You don’t need to mock ClaimsPrincipal it has no outside dependencies and you can created it un-mocked: var claims = new List<Claim>() { new Claim(ClaimTypes.Name, “username”), new Claim(ClaimTypes.NameIdentifier, “userId”), new Claim(“name”, “John Doe”), }; var identity = new ClaimsIdentity(claims, “TestAuthType”); var claimsPrincipal = new ClaimsPrincipal(identity); And I’m not sure what you are testing here. Certainly … Read more

Can not instantiate proxy…Could not find a parameterless constructor

MOQ is good for mocking interfaces, but does not work so well with concrete classes. So instead of mocking concrete class, ask for the inteface: var userStore = new Mock<IUserStore<PreRegistrationMVC.Models.ApplicationUser>>(); Also ApplicationUser should be POCO, so no need to mock it, just create its instance without MOQ and use in tests.

How to do internal interfaces visible for Moq?

The suggested fix message uses a const/static field for the assembly name: [assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)] You used a string which does not correspond to the assembly name: [assembly: InternalsVisibleTo(“InternalsVisible.DynamicProxyGenAssembly2”)] Change it to: [assembly: InternalsVisibleTo(“DynamicProxyGenAssembly2”)] You can see the actual assembly name (which should appear in InternalsVisibleTo) in your error message: Type ‘Castle.Proxies.IReminiscenceableDataTableProxy’ from assembly ‘DynamicProxyGenAssembly2 (…)

Setup Method With Params Array

You’re trying to call a method taking a single string, rather than an array. Bear in mind that it’s the C# compiler which handles the params part, converting calling code which just specifies individual values into a call passing in an array. As far as the method itself is concerned, it’s just getting an array … Read more

Setup() vs SetupGet()

Setup() can be used for mocking a method or a property. SetupGet() is specifically for mocking the getter of a property. Took a quick peek at the Moq source code and it looks like if you use Setup() on a property getter, it will call SetupGet(). So in that case, it is probably more personal … Read more

Mock.Of VS Mock()

This post helped me to understand Mock.Of<T> : Old style imperative Mock<T> vs functional Mock.Of<T> As explained in the post, with Mock.Of<T> you’re saying “Give me a mock that behaves like this” (or Mocks.Of<T> if you need to get many objects (IEnumerable)). It makes the declaration of a mock more concise. Example with Mock<T> (returns … Read more

Mock IHttpContextAccessor in Unit Tests

You can use the DefaultHttpContext as a backing for the IHttpContextAccessor.HttpContext. Saves you having to set-up too many things Next you cannot use It.IsAny<string>() as a Returns result. They were meant to be used in the set up expressions alone. Check the refactor [Fact] public async Task test_GetBookByBookId() { //Arrange //Mock IHttpContextAccessor var mockHttpContextAccessor = … Read more

Moq, strict vs loose usage

I used to use strict mocks when I first starting using mocks in unit tests. This didn’t last very long. There are really 2 reasons why I stopped doing this: The tests become brittle – With strict mocks you are asserting more than one thing, that the setup methods are called, AND that the other … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)