moq

How to test Ninject ConstructorArguments using MOQ objects?

前提是你 提交于 2019-12-03 13:34:34
问题 I have been doing my first Test Driven Development project recently and have been learning Ninject and MOQ. This is my first attempt at all this. I've found the TDD approach has been thought provoking, and Ninject and MOQ have been great. The project I am working on has not particularly been the best fit for Ninject as it is a highly configurable C# program that is designed to test the use of a web service interface. I have broken it up into modules and have interfaces all over the shop, but

Mock object returning a list of mocks with Moq

自古美人都是妖i 提交于 2019-12-03 13:29:45
I am trying to test the following code public void CleanUp() { List<ITask> tasks = _cleanupTaskFactory.GetTasks(); //Make sure each task has the task.Execute() method called on them } In my test I create a mocked implementation of _cleanupTaskFactory, and I want to stub the GetTasks() method to return a type: List<Mock<ITask>> ...but the compiler won't accept that as a return value. My goal is to ensure that each task returned has the .Execute() method called on it using the Verify() MoQ method. How can I assert that each task gets executed? In your _cleanUpTaskFactory mock you could simply do

MOQ - how to manually create a backing property using SetupGet/SetupSet?

吃可爱长大的小学妹 提交于 2019-12-03 12:56:02
I know we can call SetupAllProperties() to automatically create backing properties. But this is too restrictive, because it doesn't allow me to execute additional code in the getter/setters. For example, I'd like to create a moq'd setter that invokes some other method/event/logic. The Following code sample reproduces the issue public interface IA { int B { get; set; } }; class Test { [Test] public void BackingPropertyTest() { int b = 1; var mockA = new Mock<IA>(); //mockA.SetupAllProperties(); mockA.SetupGet(m => m.B).Returns(b); mockA.SetupSet(m => m.B).Callback(val => b = val); mockA.Object

Moq - Linq expression in repository - Specify expression in setup

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 12:18:28
I have a method on my interface that looks like: T GetSingle(Expression<Func<T, bool>> criteria); I'm trying to mock the setup something like this (I realise this isn't working): _mockUserRepository = new Mock<IRepository<User>>(); _mockUserRepository.Setup(c => c.GetSingle(x => x.EmailAddress == "a@b.com")) .Returns(new User{EmailAddress = "a@b.com"}); I realise I'm passing in the wrong parameter to the setup. After reading this answer I can get it working by passing in the Expression, like this: _mockUserRepository.Setup(c => c.GetSingle(It.IsAny<Expression<Func<User, bool>>>()) .Returns(new

Using autofac with moq

三世轮回 提交于 2019-12-03 12:17:14
I need to register my Autofac container with specific interface, for this case I want to resolved mock. How can I do it? I've tried: var AppContainer = ApplicationContainer.GetApplicationContainer(); var cb = new ContainerBuilder(); cb.RegisterType<Mock<IStudyLoader>>().As<IStudyLoader>().SingleInstance(); cb.Update(AppContainer); I don't want to change my code to resolve something else than IStudyLoader , but Mock<IStudyLoader> is not substitutable for IStudyLoader ; e.g Mock<IStudyLoader>.Object is substitutable for IStudyLoader , but I cant register Mock<IStudyLoader>.Object because it not

How do I make a unit test to test a method that checks request headers?

白昼怎懂夜的黑 提交于 2019-12-03 12:03:40
问题 I am very, very new to unit testing and am trying to write a test for a pretty simple method: public class myClass : RequireHttpsAttribute { public override void OnAuthorization(AuthoizationContext filterContext) { var request = filterContext.HttpContext.Request; var header = Convert.ToBoolean(request.Headers["Special-Header-Name"]); if (!(header || request.IsSecureConnection)) { HandleNonHttpsRequest(filterContext); } } } This method, which inherits from the RequireHttpsAttribute , checks if

Using Moq and TDD, where to start?

浪尽此生 提交于 2019-12-03 11:55:44
I have a server application and I was wondering where I should start if I want to start implementing TDD and using Moq. What good books I could read on the subject, which aren't too "web-oriented"? I have questions on the matter, like: Should I mock every object I want to test, or only those which I can't implement, like text writers? My server needs a lot of setup before it can actually do anything I want to test, should I just cram that into a [TestInitialize] function? How should I chain my tests, if I want to test deeper functionality? I recommend two books: Test Driven Development by

Partial mocking of class with Moq

江枫思渺然 提交于 2019-12-03 11:39:12
I want to mock only the GetValue method of the following class, using Moq: public class MyClass { public virtual void MyMethod() { int value = GetValue(); Console.WriteLine("ORIGINAL MyMethod: " + value); } internal virtual int GetValue() { Console.WriteLine("ORIGINAL GetValue"); return 10; } } I already read a bit how this should work with Moq. The solution that I found online is to use the CallBase property, but that doesn't work for me. This is my test: [Test] public void TestMyClass() { var my = new Mock<MyClass> { CallBase = true }; my.Setup(mock => mock.GetValue()).Callback(() => Console

How to use Unity.RegisterType with Moq?

☆樱花仙子☆ 提交于 2019-12-03 10:44:33
I have a running code with unity. Now I want to use Moq to do my unit testing for ASP-MVC. In the global.asax's code, I have the following: IUnityContainer container = new UnityContainer(); container.RegisterType<IFoo, Foo>(new InjectionConstructor("xxx")); Now I wrote testcode with Moq: IUnityContainer container = new UnityContainer(); var mockFoo = new Mock<IFoo>(); container.RegisterType<IFoo, mockFoo) >(new InjectionConstructor("xxx")); but this don't work. Error: The type 'Moq.Mock' cannot be used as type parameter 'TTo' in the generic type or method 'Microsoft.Practices.Unity

How do you use Moq to mock a simple interface?

爱⌒轻易说出口 提交于 2019-12-03 10:26:08
Okay, I have a business logic class like this: Note: For context, Vendor Briefs are simple entities that describe a "download" for a PDF document. /// <summary> /// Houses business level functions for dealing with vendor briefs. /// </summary> public class VendorBriefController : IVendorBriefController { /// <summary> /// Vendor brief controller requires an instance of IVendorBriefRepository. /// </summary> IVendorBriefRepository _vendorBriefRepository; /// <summary> /// Initializes an instance of VendorBriefController. /// </summary> public VendorBriefController(IVendorBriefRepository