moq

How do I Moq a method that has an optional argument in its signature without explicitly specifying it or using an overload?

扶醉桌前 提交于 2019-11-26 11:08:03
问题 Given the following interface: public interface IFoo { bool Foo(string a, bool b = false); } Attempting to mock it using Moq: var mock = new Mock<IFoo>(); mock.Setup(mock => mock.Foo(It.IsAny<string>())).Returns(false); gives the following error at compile time: An expression tree may not contain a call or invocation that uses optional arguments I\'ve found the issue above raised as an enhancement in Moq\'s list of issues and it appears to be assigned to the 4.5 release (whenever that is). My

Moq: unit testing a method relying on HttpContext

强颜欢笑 提交于 2019-11-26 09:35:58
问题 Consider a method in a .NET assembly: public static string GetSecurityContextUserName() { //extract the username from request string sUser = HttpContext.Current.User.Identity.Name; //everything after the domain sUser = sUser.Substring(sUser.IndexOf(\"\\\\\") + 1).ToLower(); return sUser; } I\'d like to call this method from a unit test using the Moq framework. This assembly is part of a webforms solution. The unit test looks like this, but I am missing the Moq code. //arrange string ADAccount

Using Moq to determine if a method is called

坚强是说给别人听的谎言 提交于 2019-11-26 09:28:43
问题 It is my understanding that I can test that a method call will occur if I call a higher level method, i.e.: public abstract class SomeClass() { public void SomeMehod() { SomeOtherMethod(); } internal abstract void SomeOtherMethod(); } I want to test that if I call SomeMethod() then I expect that SomeOtherMethod() will be called. Am I right in thinking this sort of test is available in a mocking framework? 回答1: You can see if a method in something you have mocked has been called by using

Is it possible to mock out a .NET HttpWebResponse?

杀马特。学长 韩版系。学妹 提交于 2019-11-26 09:19:22
问题 i\'ve got an integration test that grabs some json result from a 3rd party server. It\'s really simple and works great. I was hoping to stop actually hitting this server and using Moq (or any Mocking library, like ninject, etc) to hijack and force the return result. is this possible? Here is some sample code :- public Foo GoGetSomeJsonForMePleaseKThxBai() { // prep stuff ... // Now get json please. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(\"Http://some.fancypants.site

Mocking HttpContextBase with Moq

谁说胖子不能爱 提交于 2019-11-26 09:08:28
问题 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

How can I tell Moq to return a Task?

只谈情不闲聊 提交于 2019-11-26 08:57:49
问题 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? 回答1: Your method doesn't have any callbacks so there is no reason to

Mocking Static Methods

梦想与她 提交于 2019-11-26 07:29:45
问题 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. 回答1: Mocking frameworks like Moq or Rhinomocks can only create mock instances of objects, this means mocking static

How to mock static methods in c# using MOQ framework?

痴心易碎 提交于 2019-11-26 07:26:07
问题 I have been doing unit testing recently and I\'ve successfully mocked various scenarios using MOQ framework and MS Test. I know we can\'t test private methods but I want to know if we can mock static methods using MOQ. 回答1: Moq (and other DynamicProxy-based mocking frameworks) are unable to mock anything that is not a virtual or abstract method. Sealed/static classes/methods can only be faked with Profiler API based tools, like Typemock (commercial) or Microsoft Moles (free, known as Fakes in

How to mock an async repository with Entity Framework Core

此生再无相见时 提交于 2019-11-26 06:29:01
问题 I\'m trying to create a unit test for a class that calls into an async repository. I\'m using ASP.NET Core and Entity Framework Core. My generic repository looks like this. public class EntityRepository<TEntity> : IEntityRepository<TEntity> where TEntity : class { private readonly SaasDispatcherDbContext _dbContext; private readonly DbSet<TEntity> _dbSet; public EntityRepository(SaasDispatcherDbContext dbContext) { _dbContext = dbContext; _dbSet = dbContext.Set<TEntity>(); } public virtual

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

大城市里の小女人 提交于 2019-11-26 04:53:08
问题 or can the class be implementing an abstract class also? 回答1: 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