moq

Using Moq and TDD, where to start?

本秂侑毒 提交于 2019-12-09 09:56:58
问题 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

Using autofac with moq

血红的双手。 提交于 2019-12-09 08:44:17
问题 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>

Verifying event registration using Moq

懵懂的女人 提交于 2019-12-09 07:46:18
问题 I'm developing an asp.net (classic) application trying to implement the MVP pattern using this example. In trying to unit test my presenter and using the following pattern, the psuedocode for which looks like so //base view interface public interface IView { event EventHandler Init; event EventHandler Load; bool IsPostBack { get; } void DataBind(); bool IsValid { get;} } //presenter psuedo code public class SomePresenter { public SomePresenter(ISomeDomainService service, IView someView) { ...

Mocking 3rd party callback events using moq

不羁岁月 提交于 2019-12-09 06:27:41
问题 We've been trying write unit tests for a worker class written in C#, which mocks out a third party API (COM based) using moq to dynamically create the mock objects. NUnit is our unit testing framework. This third party component implements a couple of interfaces, but also needs to call back into our worker class using events. Our plan was to simulate the events that this 3rd party component can raise, and test that our worker class operated as expected. Unfortunately we've run into a problem

ASP.NET MVC Custom Route Constraints, Dependency Injection and Unit Testing

老子叫甜甜 提交于 2019-12-09 06:15:31
问题 About this topic, I have asked another question: ASP.NET MVC Custom Route Constraints and Dependency Injection Here is the current situation: on my ASP.NET MVC 3 App, I have a route constraint defined like below: public class CountryRouteConstraint : IRouteConstraint { private readonly ICountryRepository<Country> _countryRepo; public CountryRouteConstraint(ICountryRepository<Country> countryRepo) { _countryRepo = countryRepo; } public bool Match(HttpContextBase httpContext, Route route,

Is there any open source mocking framework resembling TypeMock? [closed]

ε祈祈猫儿з 提交于 2019-12-09 04:37:48
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last month . TypeMock is too expensive for a hobbist like me :) Moq or the next version of RhinoMocks have no plans on listening to the profiling API, why is that? EDIT: This enables features such as: Mocking non-virtual methods and properties (!). Mocking browser environments. simpler syntax which is less fragile (and not

Cannot get DbSet.Find to work with Moq (Using the Entity-Framework)

寵の児 提交于 2019-12-09 04:17:15
问题 For some reason this code keeps failing. Anyone that can tell me why: var activeLoans = new List<ActiveLoan> { new ActiveLoan{ ID = 1, CaseType = "STL", LoanCode = 0 }, new ActiveLoan{ ID = 2, CaseType = "STL", LoanCode = 0 }, new ActiveLoan{ ID = 3, CaseType = "STL", LoanCode = 0 } }.AsQueryable(); var activeLoanMockSet = new Mock<DbSet<ActiveLoan>>(); activeLoanMockSet.As<IQueryable<ActiveLoan>>().Setup(m => m.Provider).Returns(activeLoans.Provider); activeLoanMockSet.As<IQueryable

Mock IMemoryCache with Moq throwing exception

[亡魂溺海] 提交于 2019-12-09 02:20:17
问题 I'm trying to mock IMemoryCache with Moq. I'm getting this error: An exception of type 'System.NotSupportedException' occurred in Moq.dll but was not handled in user code Additional information: Expression references a method that does not belong to the mocked object: x => x.Get<String>(It.IsAny<String>()) My mocking code: namespace Iag.Services.SupplierApiTests.Mocks { public static class MockMemoryCacheService { public static IMemoryCache GetMemoryCache() { Mock<IMemoryCache>

Unit testing an HttpApplication

僤鯓⒐⒋嵵緔 提交于 2019-12-08 23:27:41
问题 I have a class derived from HttpApplication that adds some extra features. I'm to the point where I need to unit test these features, which means I have to be able to create a new instance of the HttpApplication, fake a request, and retrieve the response object. How exactly do I go about unit testing an HttpApplication object? I'm using Moq at the moment, but I have no idea how to set up the required mock object. 回答1: Unfortunately, this isn't particularly easy to do, as the HttpApplication

Setup() vs SetupGet()

折月煮酒 提交于 2019-12-08 23:16:09
问题 What is the difference between the SetupGet() and Setup() methods for MOQ? 回答1: 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 preference as to whether you want to be more explicit and use SetupGet() instead of Setup(). Of course, my knowledge of Moq