moq

.NET 4, AllowPartiallyTrustedCallers attribute, and security markings like SecurityCritical

喜夏-厌秋 提交于 2019-11-27 11:09:46
问题 I'm new C# and am trying to understand the new security features of .NET-4. To fill in some details, I'm currently trying to update AutofacContrib.Moq to work with the latest Moq. I had no problems doing this for .NET-3.5 and under. But in .NET-4 the security restrictions result in numerous security exceptions. Moq has a a single method, GetObjectData , that's marked with the SecurityCritical attribute. AutofacContrib.Moq has the AllowPartiallyTrustedCallers attribute set which is the source

Using Moq to mock an asynchronous method for a unit test

和自甴很熟 提交于 2019-11-27 10:30:36
I am testing a method for a service that makes a Web API call. Using a normal HttpClient works fine for unit tests if I also run the web service (located in another project in the solution) locally. However when I check in my changes the build server won't have access to the web service so the tests will fail. I've devised a way around this for my unit tests by creating an IHttpClient interface and implementing a version that I use in my application. For unit tests, I make a mocked version complete with a mocked asynchronous post method. Here's where I have run into problems. I want to return

Mocking using Moq in c#

核能气质少年 提交于 2019-11-27 10:22:30
问题 I have the following code: public interface IProductDataAccess { bool CreateProduct(Product newProduct); } Class ProductDataAccess implements that interface. public class ProductBusiness { public bool CreateProduct(Product newProduct) { IProductDataAccess pda = new ProductDataAccess(); bool result = pda.CreateProduct(newProduct); return result; } } In this case, how to create unit test for CreateProduct method by mocking the IProductDataAccess interface? I thought of having an public instance

Testing controller Action that uses User.Identity.Name

我们两清 提交于 2019-11-27 10:17:45
问题 I have an action that relies on User.Identity.Name to get the username of the current user to get a list of his orders: public ActionResult XLineas() { ViewData["Filtre"] = _options.Filtre; ViewData["NomesPendents"] = _options.NomesPendents; return View(_repository.ObteLiniesPedido(User.Identity.Name,_options.Filtre,_options.NomesPendents)); } Now I'm trying to write unit tests for this, but I get stuck on how to provide a Mock for User.Identity.Name. If I run my test as I have it (without

Proper way to Mock repository objects for unit tests using Moq and Unity

杀马特。学长 韩版系。学妹 提交于 2019-11-27 09:35:05
问题 At my job we are using Moq for mocking and Unity for an IOC container. I am fairly new to this and do not have many resources at work to help me out with determining the best practices I should use. Right now, I have a group of repository interfaces (Ex: IRepository1, IRepository2... IRepository4) that a particular process needs to use to do its job. In the actual code I can determine all of the IRepository objects by using the IOC container and using the RegisterType() method. I am trying to

How to test method call order with Moq

大憨熊 提交于 2019-11-27 09:01:08
At the moment I have: [Test] public void DrawDrawsAllScreensInTheReverseOrderOfTheStack() { // Arrange. var screenMockOne = new Mock<IScreen>(); var screenMockTwo = new Mock<IScreen>(); var screens = new List<IScreen>(); screens.Add(screenMockOne.Object); screens.Add(screenMockTwo.Object); var stackOfScreensMock = new Mock<IScreenStack>(); stackOfScreensMock.Setup(s => s.ToArray()).Returns(screens.ToArray()); var screenManager = new ScreenManager(stackOfScreensMock.Object); // Act. screenManager.Draw(new Mock<GameTime>().Object); // Assert. screenMockOne.Verify(smo => smo.Draw(It.IsAny

Unit test to verify that a base class method is called

走远了吗. 提交于 2019-11-27 08:11:48
问题 I have a base class: public abstract class MyBaseClass { protected virtual void Method1() { } } and a derived class: public class MyDerivedClass : MyBaseClass { public void Method2() { base.Method1(); } } I want to write a unit test for Method2 to verify that it calls Method1 on the base class. I'm using Moq as my mocking library. Is this possible? I came across a related SO link: Mocking a base class method call with Moq in which the 2nd answer suggests it can be achieved by setting CallBase

Mocking a TempData in ASP.NET Core in MSTest

家住魔仙堡 提交于 2019-11-27 08:08:26
问题 public ActionResult View(string name) { if (TempData["SessionVariable"] != null) { FileName = name; return View(); } else { return RedirectToAction("index", "Home"); } } TestMethod public void UseCaseView_CorrectRequirements() { var mock = new Mock<Controller>(); mock.Setup(p => p.TempData["SessionVariable"]).Returns("admin"); Controller.View("SAMPLE.xml"); } It throws an error such as , Result StackTrace: at Moq.Mock.ThrowIfSetupExpressionInvolvesUnsupportedMember(Expression setup,

Using Moq to verify calls are made in the correct order

纵饮孤独 提交于 2019-11-27 07:56:37
I need to test the following method: CreateOutput(IWriter writer) { writer.Write(type); writer.Write(id); writer.Write(sender); // many more Write()s... } I've created a Moq'd IWriter and I want to ensure that the Write() methods are called in the right order. I have the following test code: var mockWriter = new Mock<IWriter>(MockBehavior.Strict); var sequence = new MockSequence(); mockWriter.InSequence(sequence).Setup(x => x.Write(expectedType)); mockWriter.InSequence(sequence).Setup(x => x.Write(expectedId)); mockWriter.InSequence(sequence).Setup(x => x.Write(expectedSender)); However, the

Mocking EF DbContext with Moq

丶灬走出姿态 提交于 2019-11-27 06:57:17
I'm trying to create a unit test for my service with a mocked DbContext. I created an interface IDbContext with the following functions: public interface IDbContext : IDisposable { IDbSet<T> Set<T>() where T : class; DbEntityEntry<T> Entry<T>(T entity) where T : class; int SaveChanges(); } My real context implements this interface IDbContext and DbContext . Now I'm trying to mock the IDbSet<T> in the context, so it returns a List<User> instead. [TestMethod] public void TestGetAllUsers() { // Arrange var mock = new Mock<IDbContext>(); mock.Setup(x => x.Set<User>()) .Returns(new List<User> { new