moq

When mocking a class with Moq, how can I CallBase for just specific methods?

天大地大妈咪最大 提交于 2019-11-27 21:49:08
I really appreciate Moq's Loose mocking behaviour that returns default values when no expectations are set. It's convenient and saves me code, and it also acts as a safety measure: dependencies won't get unintentionally called during the unit test (as long as they are virtual). However, I'm confused about how to keep these benefits when the method under test happens to be virtual. In this case I do want to call the real code for that one method, while still having the rest of the class loosely mocked. All I have found in my searching is that I could set mock.CallBase = true to ensure that the

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

落花浮王杯 提交于 2019-11-27 21:20:29
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<IEnumerable<T>> ExecuteQueryAsync(IQueryable<T> query) { IDocumentQuery<T> documentQuery = query

How to Mock the Internal Method of a class?

一笑奈何 提交于 2019-11-27 21:14:56
I have a class which has a internal method and i want to mock the internal method . But i am unable to mock it i.e. it is not calling the mocked function but calling the original function. Is there any way to achieve this ? Edit:Actually i am a novice to the Moq. I have many classes and methods of the classes to test using the Moq. Many classes are internal , many have internal methods, many have not-virtual methods . And can not change the signature on the methods and classes. Can anyone please let me know how to go about testing this scenario using Moq. Or else please suggest me some other

Moq and SqlConnection?

和自甴很熟 提交于 2019-11-27 21:01:12
问题 I'm writing unit tests for one of our products and have been used Moq to successfully mock connections to Entity Framework. However, I've come across the following method: public static productValue findValues(string productName, string dbConnectionString) { try { SqlConnection conn = new SqlConnection(dbConnectionString); conn.Open(); //Do stuff } } Which accesses our database inside that method using a passed connection string. Is it possible to setup a mock DB using Moq and create a

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

大城市里の小女人 提交于 2019-11-27 20:28:51
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 instead of FileInfo): private IFileInfo _logFile; public LogEventProcessorTextFile(IFileInfo logFile) {

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

帅比萌擦擦* 提交于 2019-11-27 19:37:24
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 Mock<IEnumerable<ICarrierApiService<AccountSearchModel>>>(); _mockApiService.Setup(x => x

SetupSequence in Moq

非 Y 不嫁゛ 提交于 2019-11-27 19:35:29
I want a mock returns a 0 the first time, then returns 1 anytime the method was called. The problem is that if the method is called 4 times, I should write that : mock.SetupSequence(x => x.GetNumber()) .Returns(0) .Returns(1) .Returns(1) .Returns(1); otherwise the method returns null. Is there any way to write that the next times the method was called after the first time, the method returns 1 ? Thank you Is it good to have more "operators" for SetupSequence ? If you think YES you can vote : http://moq.uservoice.com/forums/11304-general/suggestions/2973521-setupsequence-more-operators That's

Moq.Mock<T> - how to set up a method that takes an expression

↘锁芯ラ 提交于 2019-11-27 18:00:43
I am Mocking my repository interface and am not sure how to set up a method that takes an expression and returns an object? I am using Moq and NUnit. Interface: public interface IReadOnlyRepository : IDisposable { IQueryable<T> All<T>() where T : class; T Single<T>(Expression<Func<T, bool>> expression) where T : class; } Test with IQueryable is already set up, but don't know how to set up the T Single: private Moq.Mock<IReadOnlyRepository> _mockRepos; private AdminController _controller; [SetUp] public void SetUp() { var allPages = new List<Page>(); for (var i = 0; i < 10; i++) { allPages.Add

Moq'ing methods where Expression<Func<T, bool>> are passed in as parameters

你离开我真会死。 提交于 2019-11-27 17:41:02
I'm very new to unit testing and mocking! I'm trying to write some unit tests that covers some code that interacts with a data store. Data access is encapsulated by IRepository: interface IRepository<T> { .... IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate); .... } The code that I'm trying to test, utilising a concrete IoC'd implementation of IRepository looks like this: public class SignupLogic { private Repository<Company> repo = new Repository<Company>(); public void AddNewCompany(Company toAdd) { Company existingCompany = this.repo.FindBy(c => c.Name == toAdd.Name)

Mock HttpRequest in ASP.NET Core Controller

℡╲_俬逩灬. 提交于 2019-11-27 17:40:51
问题 I'm building a Web API in ASP.NET Core, and I want to unit test the controllers. I inject an interface for data access, that I can easily mock. But the controller has to check the headers in the Request for a token, and that Request doesn't seem to exist when I simply instantiate the controller myself, and it is also get-only, so I can't even manually set it. I found lots of examples to mock an ApiController, but that isn't .NET core. Also many tutorials and examples of how to unit test .net