moq

ASP.NET Testing Api controller: Uri(Request.GetEncodedUrl()…) returns null

帅比萌擦擦* 提交于 2021-02-19 07:43:49
问题 I am testing an ASP.NET Core Api post method, but I get a System.NullReferenceException causing my test to fail. The null exception appears when I try to return a Created()-ActionResult, like this: return Created(new Uri(Request.GetEncodedUrl() + "/" + personDto.Id), personDto); My personDto.Id nor the personDto is not null, but the result of the Uri() returns null. I have googled but not found a solution, but I believe I have to mock the Uri()-method in some way (I am new to testing and asp

Callback of Moq is not called?

做~自己de王妃 提交于 2021-02-11 13:41:02
问题 In the following code. The Assert.Equal(...) of the Callback() is never called? var test = "Test"; var command = new MyCommand { V = test }; var mock = new Mock<IRepository>(); // IRepository has the method of Save() var p = new P(test); mock.Setup(x => x.Save(p)) .Callback<P>(x => Assert.Equal(x.Value, test)); // break point on Assert.Equal not hit var sut = new C(mock.Object); var result = await sut.M(command); 回答1: You have: .Setup(x => x.Save(p)) but are you sure the P used in your SUT is

Azure CloudFile DownloadToStream method mocking with Callback not working

北城余情 提交于 2021-02-11 12:33:33
问题 This is the code for reading file from azure file storage and process the data. I am using latest file storage nuget packages. CloudStorageAccount storageAccount = CloudStorageAccount.Parse("FileStorageConnectionString"); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare cloudShare = fileClient.GetShareReference("FileShareName"); var cloudFile = this.cloudShare.GetRootDirectoryReference().GetFileReference("file.txt"); var memoryStream = new MemoryStream();

Moq It.IsAnyType not working for Func returning Task with generic type

微笑、不失礼 提交于 2021-02-10 07:53:51
问题 I've tried 3 different ways to setup a mock for a generic interface method. Only one way works, but it is using an explicit type, so won't work generically. I tried using It.IsAnyType, but it doesn't seem to match on the call that is made. Here is the sample code (I expected test case 1 to have "asdf" returned). How can I get the mock to work for any type (not just string?)? using Moq; using System; using System.Threading.Tasks; namespace blah { public class Junk : IGetOrAddable { public

How to use Moq framework to unit test azure service fabrics?

[亡魂溺海] 提交于 2021-02-08 15:36:37
问题 I am planning to use Moq for unit testing my azure service fabric application. I saw some of the examples here https://github.com/Azure-Samples/service-fabric-dotnet-web-reference-app/blob/master/ReferenceApp/Inventory.UnitTests/InventoryServiceTests.cs. The test I saw seems like actually writing to reliable dictionary and not mocking. Is there way to mock the add/remove from reliable dictionary? How do I unit test something like below public async Task<bool> AddItem(MyItem item) { var items

Passing Moqs It.IsAny<string> as method argument

五迷三道 提交于 2021-02-08 15:01:27
问题 First some information about my development environment: .Net Framework 4.5 Moq 4.10 Autofac 4.2 NUnit 3.11 I try to mock a function that takes some string arguments and I would like to use It.IsAny<string>() to setup. Normally I would to that like this: using ( AutoMock mock = AutoMock.GetLoose() ) { mock.Mock<SomeInterface>() .Setup( x => x.someFunction( It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>() ) ); //... } But now I would like to call a fucntion that makes the setup, so I

How would I use moq to test a MongoDB service layer?

懵懂的女人 提交于 2021-02-07 11:54:43
问题 I have a service layer between my app and the mongo database. I'm trying to build a unit test using moq I'm quite new to moq so I started with what I thought would be a trivial test. Code to test: public List<BsonDocument> GetAllSettings() { var collection = MongoDatabase.GetCollection<BsonDocument>("Settings"); var query = from e in collection.AsQueryable() select e; var settings = query.ToList(); return settings; } Where: Settings is a Collection MongoDatabase is a MongoDBDriver

Mocking MediatR 3 with Moq

陌路散爱 提交于 2021-02-06 15:24:18
问题 We've recently started using MediatR to allow us to de-clutter controller actions as we re-factor a large customer facing portal and convert it all to C#. As part of this we are increasing our unit test coverage as well, but I've hit a problem when trying to mock MediatR itself. The command does a bunch of stuff to initiate a process and part of this is sending a notification. The notification itself is dealt with by its own handler and therefore would be subject to its own unit test so I

Mocking MediatR 3 with Moq

心已入冬 提交于 2021-02-06 15:19:00
问题 We've recently started using MediatR to allow us to de-clutter controller actions as we re-factor a large customer facing portal and convert it all to C#. As part of this we are increasing our unit test coverage as well, but I've hit a problem when trying to mock MediatR itself. The command does a bunch of stuff to initiate a process and part of this is sending a notification. The notification itself is dealt with by its own handler and therefore would be subject to its own unit test so I

Moq - What happens when using It.IsAny in a setup's return?

ⅰ亾dé卋堺 提交于 2021-02-04 07:29:45
问题 I am performing unit tests in C# using Moq. One test in particular I have created an interface wrapper over System.Net.Mail.SmtpClient so that it can be mocked. public class SmtpClient : ISmtpClient { public string Host { get; set; } public int Port { get; set; } public ICredentialsByHost Credentials { get; set; } public bool EnableSsl { get; set; } public void Send(MailMessage mail) { var smtpClient = new System.Net.Mail.SmtpClient { Host = Host, Port = Port, Credentials = Credentials,