moq

Mock static property with moq

只愿长相守 提交于 2019-11-27 00:54:12
I am pretty new to use moq . I am into creating some unit test case to HttpModule and everything works fine until I hit a static property as follows this.applicationPath = (HttpRuntime.AppDomainAppVirtualPath.Length > 1) ? HttpRuntime.AppDomainAppVirtualPath : String.Empty; I do not know how create mocks for static class and property like HttpRuntime.AppDomainAppVirtualPath . The context , request and response have been mocked well with sample code I get from moq. I will appreciate if somebody can help me on this. Moq can't fake static members. As a solution you can create a wrapper class

How to assign values to properties in moq?

泄露秘密 提交于 2019-11-27 00:50:20
问题 I have a class with a method that returns an object of type User public class CustomMembershipProvider : MembershipProvider { public virtual User GetUser(string username, string password, string email, bool isApproved) { return new User() { Name = username ,Password = EncodePassword(password) ,Email = email ,Status = (isApproved ? UsuarioStatusEnum.Ativo : UsuarioStatusEnum.ConfirmacaoPendente) // ... }; } // .. } User is a domain object. Note the Id property with setter as protected : public

Using Moq to determine if a method is called

独自空忆成欢 提交于 2019-11-27 00:41:15
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? Paul You can see if a method in something you have mocked has been called by using Verify, e.g.: static void Main(string[] args) { Mock<ITest> mock = new Mock<ITest>(); ClassBeingTested

How do I verify a method was called exactly once with Moq?

余生长醉 提交于 2019-11-27 00:39:55
问题 How do I verify a method was called exactly once with Moq? The Verify() vs. Verifable() thing is really confusing. 回答1: You can use Times.Once() , or Times.Exactly(1) : mockContext.Verify(x => x.SaveChanges(), Times.Once()); mockContext.Verify(x => x.SaveChanges(), Times.Exactly(1)); Here are the methods on the Times class: AtLeast - Specifies that a mocked method should be invoked times times as minimum. AtLeastOnce - Specifies that a mocked method should be invoked one time as minimum.

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

送分小仙女□ 提交于 2019-11-27 00:21:27
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. Igal Tabachnik 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 Visual Studio 2012 Ultimate /2013 /2015). Alternatively, you could refactor your design to

Is it possible to mock out a .NET HttpWebResponse?

筅森魡賤 提交于 2019-11-27 00:16:53
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/api/hiThere); httpWebRequest.Method = WebRequestMethods.Http.Get; string responseText; using (var

How to mock an async repository with Entity Framework Core

╄→尐↘猪︶ㄣ 提交于 2019-11-27 00:08:49
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 IQueryable<TEntity> GetAll() { return _dbSet; } public virtual async Task<TEntity> FindByIdAsync(int id) {

How do you mock the session object collection using Moq

[亡魂溺海] 提交于 2019-11-27 00:02:55
I am using shanselmann's MvcMockHelper class to mock up some HttpContext stuff using Moq but the issue I am having is being able to assign something to my mocked session object in my MVC controller and then being able to read that same value in my unit test for verification purposes. My question is how do you assign a storage collection to the mocked session object to allow code such as session["UserName"] = "foo" to retain the "foo" value and have it be available in the unit test. I started with Scott Hanselman's MVCMockHelper , added a small class and made the modifications shown below to

Returning value that was passed into a method

有些话、适合烂在心里 提交于 2019-11-26 23:51:45
问题 I have a method on an interface: string DoSomething(string whatever); I want to mock this with MOQ, so that it returns whatever was passed in - something like: _mock.Setup( theObject => theObject.DoSomething( It.IsAny<string>( ) ) ) .Returns( [the parameter that was passed] ) ; Any ideas? 回答1: You can use a lambda with an input parameter, like so: .Returns((string myval) => { return myval; }); Or slightly more readable: .Returns<string>(x => x); 回答2: Even more useful, if you have multiple

Mocking HttpPostedFileBase and InputStream for unit-test

感情迁移 提交于 2019-11-26 23:14:19
问题 I want to test the following line of code: ... Bitmap uploadedPicture = Bitmap.FromStream(model.Picture.InputStream) as Bitmap; ... Picture is a property in my model type HttpPostedFileBase. So I would like to mock a HttpPostedFileBase property for unit-testing: model.Picture = new Mock<HttpPostedFileBase>().Object; No problem at all. Now I have to mock the InputStream, otherwise it's null: model.Picture.InputStream = new Mock<Stream>().Object; This isn't working as the InputStream is read