moq

Mocking Asp.net-mvc Controller Context

不打扰是莪最后的温柔 提交于 2019-11-26 15:05:13
So the controller context depends on some asp.net internals. What are some ways to cleanly mock these up for unit tests? Seems like its very easy to clog up tests with tons of setup when I only need, for example, Request.HttpMethod to return "GET". I've seen some examples/helpers out on the nets, but some are dated. Figured this would be a good place to keep the latest and greatest. I'm using latest version of rhino mocks Haacked Using MoQ it looks something like this: var request = new Mock<HttpRequestBase>(); request.Expect(r => r.HttpMethod).Returns("GET"); var mockHttpContext = new Mock

Moq and setting up DB Context

早过忘川 提交于 2019-11-26 14:56:54
问题 I have an Entity Framework DB Context file. I am trying to setup a Moq framework in NUnit. Currently receiving error below for Moq Nunit test. How would I setup the DBContext, and add items to a Product Table? "No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a

How to test method call order with Moq

时间秒杀一切 提交于 2019-11-26 14:26:40
问题 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.

How to mock the Request on Controller in ASP.Net MVC?

懵懂的女人 提交于 2019-11-26 14:07:36
I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{ public ActionResult Index() { if (Request.IsAjaxRequest()) { //do some ajaxy stuff } return View("Index"); } } I got some tips on mocking and was hoping to test the code with the following and RhinoMocks var mocks = new MockRepository(); var mockedhttpContext = mocks.DynamicMock<HttpContextBase>(); var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>(); SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest); var controller = new HomeController(); controller.ControllerContext =

Using Moq to verify calls are made in the correct order

为君一笑 提交于 2019-11-26 13:55:56
问题 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

Mocking EF DbContext with Moq

僤鯓⒐⒋嵵緔 提交于 2019-11-26 13:00:21
问题 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() { /

Mock static property with moq

痞子三分冷 提交于 2019-11-26 12:25:36
问题 I am pretty new to use moq. I am into creating some unit test case to HttpModule and everything works fine until I hit a static property as follows this.applicationPath = (HttpRuntime.AppDomainAppVirtualPath.Length > 1) ? HttpRuntime.AppDomainAppVirtualPath : String.Empty; I do not know how create mocks for static class and property like HttpRuntime.AppDomainAppVirtualPath . The context , request and response have been mocked well with sample code I get from moq. I will appreciate if somebody

How do you mock the session object collection using Moq

一世执手 提交于 2019-11-26 12:21:37
问题 I am using shanselmann\'s MvcMockHelper class to mock up some HttpContext stuff using Moq but the issue I am having is being able to assign something to my mocked session object in my MVC controller and then being able to read that same value in my unit test for verification purposes. My question is how do you assign a storage collection to the mocked session object to allow code such as session[\"UserName\"] = \"foo\" to retain the \"foo\" value and have it be available in the unit test. 回答1

Moq + Unit Testing - System.Reflection.TargetParameterCountException: Parameter count mismatch

不问归期 提交于 2019-11-26 11:28:47
问题 I\'m tring to use a lambda with a multiple-params function but Moq throws this exception at runtime when I attempt to call the mock.Object.Convert(value, null, null, null); line. System.Reflection.TargetParameterCountException: Parameter count mismatch The code is: var mock = new Mock<IValueConverter>(); mock.Setup(conv => conv.Convert(It.IsAny<Object>(), It.IsAny<Type>(), It.IsAny<Object>(), It.IsAny<CultureInfo>())).Returns((Int32 num) => num + 5); var value = 5; var expected = 10; var

What is the difference between passing It.IsAny<int>() and the value of It.IsAny<int>() to a method setup

微笑、不失礼 提交于 2019-11-26 11:28:36
问题 I\'m using Moq and want to create builder classes to create my mocks with preset reasonable defaults that can be overridden during test setup as needed. The approach I took uses extension methods in which I pass input parameter values and expected output. In doing so, I\'m seeing different behavior in what seems to me to be semantically equivalent code: passing It.IsAny() directly in a setup vs passing the value of It.IsAny() indirectly in a setup. Example: public interface IFoo { bool Bar