moq

Usage of Moq When(Func<bool>) method

廉价感情. 提交于 2019-12-21 03:13:26
问题 I can't find an example of the usage of the When method in Moq When(Func<bool> condition); What is the purpose/usage of the method? Please give a code sample demonstrating a scenario where it would be useful. 回答1: "When" gives you the option to have different setups for the same mocked object, depending on whatever you have to decide. Let's say you want to test a format provider you have written. If the program (= test) runs in the morning a certain function call should return null; in the

How to add claims in a mock ClaimsPrincipal

谁都会走 提交于 2019-12-21 03:11:05
问题 I am trying to unit test my controller code which gets the information from the ClaimsPrincipal.Current. In the controller code I public class HomeController { public ActionResult GetName() { return Content(ClaimsPrincipal.Current.FindFirst("name").Value); } } And I am trying to mock my ClaimsPrincipal with claims but I still don't have any mock value from the claim. // Arrange IList<Claim> claimCollection = new List<Claim> { new Claim("name", "John Doe") }; var identityMock = new Mock

How to set up a method twice for different parameters with Moq

房东的猫 提交于 2019-12-20 15:44:16
问题 I would like to set up a method with Moq twice but it seems that the last one overrides the previous ones. Here's my initial setup: string username = "foo"; string password = "bar"; var principal = new GenericPrincipal( new GenericIdentity(username), new[] { "Admin" }); var membershipServiceMock = new Mock<IMembershipService>(); membershipServiceMock.Setup(ms => ms.ValidateUser(username, password) ).Returns(new ValidUserContext { Principal = principal }); This works out fine but I want this

How do I go about unit testing with Entity Framework and Moq?

情到浓时终转凉″ 提交于 2019-12-20 15:22:20
问题 I'm new to Moq, and wanting to use it like a backing store for data - but without touching the live database. My setup is as follows: A UnitOfWork contains all repositories, and is used for data access throughout the application. A Repository represents a direct hook into a DbSet, provided by a DbContext. A DbContext contains all DbSets. Here is my test so far: // ARRANGE var user = new User() { FirstName = "Some", LastName = "Guy", EmailAddress = "some.guy@mockymoqmoq.com", }; var mockSet =

How do I go about unit testing with Entity Framework and Moq?

流过昼夜 提交于 2019-12-20 15:21:03
问题 I'm new to Moq, and wanting to use it like a backing store for data - but without touching the live database. My setup is as follows: A UnitOfWork contains all repositories, and is used for data access throughout the application. A Repository represents a direct hook into a DbSet, provided by a DbContext. A DbContext contains all DbSets. Here is my test so far: // ARRANGE var user = new User() { FirstName = "Some", LastName = "Guy", EmailAddress = "some.guy@mockymoqmoq.com", }; var mockSet =

Unit testing with Moq, Silverlight and NUnit

青春壹個敷衍的年華 提交于 2019-12-20 10:41:31
问题 I am attempting to unit test a Silverlight 3 project. I am using: Moq.Silverlight (3.0.308.2) NUnitSilverlight (http://www.jeff.wilcox.name/2009/01/nunit-and-silverlight/) When I write a test that does not use Moq, it works as it should. When I use Moq outside of a test, Moq works as it should. (I mocked a interface and did a verify in a button handler as a proof.) But when I run a unit test that uses Moq, I always get this: System.IO.FileNotFoundException: Could not load file or assembly

How to do internal interfaces visible for Moq?

时间秒杀一切 提交于 2019-12-20 09:57:27
问题 I have 3 project in my C# solution. Signatures Structures Tests Signatures has public and internal interfaces. Also it has [assembly: InternalsVisibleTo("Structures")] [assembly: InternalsVisibleTo("Tests")] in AssemblyInfo.cs of. Structures has public and internal classes and [assembly: InternalsVisibleTo("Tests")] in AssemblyInfo.cs of. Tests has next source: <packages> <package id="Moq" version="4.2.1409.1722" targetFramework="net45" /> <package id="NUnit" version="2.6.4" targetFramework=

Different return values the first and second time with Moq

荒凉一梦 提交于 2019-12-20 07:58:30
问题 I have a test like this: [TestCase("~/page/myaction")] public void Page_With_Custom_Action(string path) { // Arrange var pathData = new Mock<IPathData>(); var pageModel = new Mock<IPageModel>(); var repository = new Mock<IPageRepository>(); var mapper = new Mock<IControllerMapper>(); var container = new Mock<IContainer>(); container.Setup(x => x.GetInstance<IPageRepository>()).Returns(repository.Object); repository.Setup(x => x.GetPageByUrl<IPageModel>(path)).Returns(() => pageModel.Object);

Moq Returns with multiple Linq Expressions

白昼怎懂夜的黑 提交于 2019-12-20 05:31:32
问题 I have the following method within a repository that I am trying to Mock: IEnumerable<TEntity> GetAll( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") I've setup the following: mockContactNumberRepository.Setup(x => x.GetAll( It.IsAny<Expression<Func<ContactNumber, bool>>>(), It.IsAny<Func<IQueryable<ContactNumber>, IOrderedQueryable<ContactNumber>>>(), It.IsAny<string>())) .Returns(new Func

IOC on IValidationDictionary with Castle Windsor

白昼怎懂夜的黑 提交于 2019-12-20 02:58:26
问题 I'm new to Castle Windsor and am just using the latest version. I've created entries for my repositories which are working fine but I have one final dependency that I'm passing into my controller. I've created a ModelStateWrapper which inherits from IValidationDictionary. The ModelStateWrapper takes a ModelStateDictionary in it's constructor so that in my code I can pass the following as an example: IMembershipService _memSvc; IValidationDictionary _validationService; public AccountController