moq

Mocking HttpContextBase with Moq

限于喜欢 提交于 2019-11-26 22:08:57
I have a unit test fixture in which I'm trying to test a ControllerAction on an ASP.NET MVC controller that's used for membership functions on a web app. I'm trying to mock the HttpContext for the tests. The ControllerAction under test actually sets properties on the HttpContext, such as Session values, Response.Cookies values, etc. This isn't all of the code, but here is a rough sample of the test that I'm trying to get to run: [Test] public void ValidRegistrationDataSuccessfullyCreatesAndRegistersUser() { var context = new Mock<HttpContextBase>() {DefaultValue = DefaultValue.Mock}; context

Mocking Static Methods

元气小坏坏 提交于 2019-11-26 22:04:28
Recently, I've begun to use Moq to unit test. I use Moq to mock out classes that I don't need to test. How do you typically deal with static methods? public void foo(string filePath) { File f = StaticClass.GetFile(filePath); } How could this static method, StaticClass.GetFile() get mocked? P.S. I'd appreciate any reading materials you recommend on Moq and Unit Testing. Pure.Krome Mocking frameworks like Moq or Rhinomocks can only create mock instances of objects, this means mocking static methods is not possible. You can also search Google for more info. Also, there's a few questions

How to (should I) mock DocumentClient for DocumentDb unit testing?

吃可爱长大的小学妹 提交于 2019-11-26 20:36:18
问题 From the new CosmosDb emulator I got sort of a repository to perform basic documentdb operations, this repository gets injected to other classes. I wanted to unit test a basic query. public class DocumentDBRepository<T> where T : class { //Details ommited... public IQueryable<T> GetQueryable() { return _client.CreateDocumentQuery<T>( UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId), new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true }); } public async Task

How do I MOQ the System.IO.FileInfo class… or any other class without an interface?

纵然是瞬间 提交于 2019-11-26 20:22:46
问题 I am writing a number of unit tests for a logger class I created and I want to simulate the file class. I can't find the interface that I need to use to create the MOQ... so how do you successfully MOQ a class without an interface? It also isn't clear to me how I can use dependency injection without having an interface available: private FileInfo _logFile; public LogEventProcessorTextFile(FileInfo logFile) { _logFile = logFile; } When I really want to do something like this (note IFileInfo

Using moq to mock only some methods

风格不统一 提交于 2019-11-26 19:58:46
问题 I have the following method: public CustomObect MyMethod() { var lUser = GetCurrentUser(); if (lUser.HaveAccess) { //One behavior } else { //Other behavior } //return CustomObject } I want to mock IMyInterface.GetCurrentUser , so that while calling MyMethod I could get to one of the code paths to check it. How to do that with Moq? I'm doing the following thing: var moq = new Mock<IMyInterface>(); moq.Setup(x => x.GetCurrentUser()).Returns(lUnauthorizedUser); //act var lResult = moq.Object

Expression references a method that does not belong to the mocked object

怎甘沉沦 提交于 2019-11-26 19:56:55
问题 I have an api service that calls another api service. When I set up the Mock objects, it failed with an error: NotSupportedException: expression references a method that does not belong to the mocked object. This is the code: private Mock<IEnumerable<ICarrierApiService<AccountSearchModel>>> _mockCarrierService; private Mock<IApiService<AccountSearchModel>> _mockApiService; [SetUp] public void SetUp() { _mockApiService = new Mock<IApiService<AccountSearchModel>>(); _mockCarrierService = new

How can I tell Moq to return a Task?

本小妞迷上赌 提交于 2019-11-26 19:26:01
I've got an interface which declares Task DoSomethingAsync(); I'm using MoqFramework for my tests: [TestMethod()] public async Task MyAsyncTest() { Mock<ISomeInterface> mock = new Mock<ISomeInterface>(); mock.Setup(arg => arg.DoSomethingAsync()).Callback(() => { <my code here> }); ... } Then in my test I execute the code which invokes await DoSomethingAsync() . And the test just fails on that line. What am I doing wrong? Panagiotis Kanavos Your method doesn't have any callbacks so there is no reason to use .CallBack() . You can simply return a Task with the desired values using .Returns() and

How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

怎甘沉沦 提交于 2019-11-26 17:03:24
问题 .NET Core 2.1 comes with this new factory called HttpClientFactory , but I can't figure out how to mock it to unit test some methods that include REST service calls. The factory is being injected using .NET Core IoC container, and what the method does is create a new client from the factory: var client = _httpClientFactory.CreateClient(); And then using the client to get data from a REST service: var result = await client.GetStringAsync(url); 回答1: The HttpClientFactory is derived from

To mock an object, does it have to be either implementing an interface or marked virtual?

自古美人都是妖i 提交于 2019-11-26 16:41:36
or can the class be implementing an abstract class also? To mock a type, it must either be an interface (this is also called being pure virtual ) or have virtual members (abstract members are also virtual). By this definition, you can mock everything which is virtual . Essentially, dynamic mocks don't do anything you couldn't do by hand . Let's say you are programming against an interface such as this one: public interface IMyInterface { string Foo(string s); } You could manually create a test-specific implementation of IMyInterface that ignores the input parameter and always returns the same

Mocking HttpClient in unit tests

ぐ巨炮叔叔 提交于 2019-11-26 15:53:29
I have some issues trying to wrap my code to be used in unit tests. The issues is this. I Have the interface IHttpHandler: public interface IHttpHandler { HttpClient client { get; } } And the class using it, HttpHandler: public class HttpHandler : IHttpHandler { public HttpClient client { get { return new HttpClient(); } } } And then the Connection class, which uses simpleIOC to inject the client implementation: public class Connection { private IHttpHandler _httpClient; public Connection(IHttpHandler httpClient) { _httpClient = httpClient; } } And then I have a unit test project which has