moq

How to assign values to properties in moq?

时光毁灭记忆、已成空白 提交于 2019-11-28 05:12:53
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 class User : IAuditable, IUser { public virtual int Id { get; protected set; } public virtual string

Moq Event Aggregator Is it possible

久未见 提交于 2019-11-28 04:47:47
问题 Wondering if its possible to Moq the Prism EventAggregator Let's take the EventAggregator Quickstart they have [TestMethod] public void PresenterPublishesFundAddedOnViewAddClick() { var view = new MockAddFundView(); var EventAggregator = new MockEventAggregator(); var mockFundAddedEvent = new MockFundAddedEvent(); EventAggregator.AddMapping<FundAddedEvent>(mockFundAddedEvent); var presenter = new AddFundPresenter(EventAggregator); presenter.View = view; view.Customer = "99"; view.Fund =

Returning value that was passed into a method

偶尔善良 提交于 2019-11-28 02:51:51
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? mhamrah You can use a lambda with an input parameter, like so: .Returns((string myval) => { return myval; }); Or slightly more readable: .Returns<string>(x => x); Steve Even more useful, if you have multiple parameters you can access any/all of them with: _mock.Setup(x => x.DoSomething(It.IsAny<string>(),It

Moq how to correctly mock set only properties

给你一囗甜甜゛ 提交于 2019-11-28 02:44:40
问题 What is the correct way for dealing with interfaces the expose set-only properties with Moq? Previously I've added the other accessor but this has bled into my domain too far with random throw new NotImplementedException() statements throughout. I just want to do something simple like: mock.VerifySet(view => view.SetOnlyValue, Times.Never()); But this yields a compile error of The property 'SetOnlyValue' has no getter 回答1: public class Xyz { public virtual string AA { set{} } } public class

Need some advice for trying to mock a .NET WebClient or equivalent

懵懂的女人 提交于 2019-11-28 02:34:03
问题 I've got some code which downloads some RSS feeds. I've been using WebClient or Argotic.Syndication.RssFeed libraries. But these aren't mockable :( I definately do not want to hit the real RSS feed every time I run the unit test. Does anyone have any suggestions to what I can do? Do I need to create an evil wrapper? If so .. suggestions on this? 回答1: I go for creating a wrapper for every external dependency (if it's practical). So, every interaction with the filesystem/a webservice/database

access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed

烈酒焚心 提交于 2019-11-28 02:32:04
问题 I have problem with unit testing my WEB API controller, I'm using moq to mock up my repository, do the setup and response for it. Then initiate the controller with mocked repository. The problem is when I try to execute a call from the controller I get an exception: Attempt by method 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' to access method 'System.Web.Http.HttpConfiguration.DefaultFormatters()' failed. at System.Web.Http.HttpConfiguration..ctor

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

一笑奈何 提交于 2019-11-28 01:18:31
.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); The HttpClientFactory is derived from IHttpClientFactory Interface So it is just a matter of creating a mock of the interface var mockFactory = new Mock

Mocking a base class method call with Moq

╄→гoц情女王★ 提交于 2019-11-28 00:41:37
I am modifiying a class method which formats some input paramater dates which are subsequently used as params in a method call into the base class (which lives in another assembly). I want to verify that the dates i pass in to my method are in the correct format when they are passed to the base class method so i would like to Moq the base class method call. Is this possible with Moq? As of 2013 with latest Moq you can. Here is an example public class ViewModelBase { public virtual bool IsValid(DateTime date) { //some complex shared stuff here } } public class MyViewModel : ViewModelBase {

Mocking HttpPostedFileBase and InputStream for unit-test

喜你入骨 提交于 2019-11-27 22:48:30
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-only (hasn't a setter method): public virtual Stream InputStream { get; } Is there a good and clean way

How to MOQ an Indexed property

谁说我不能喝 提交于 2019-11-27 22:38:45
I am attempting to mock a call to an indexed property. I.e. I would like to moq the following: object result = myDictionaryCollection["SomeKeyValue"]; and also the setter value myDictionaryCollection["SomeKeyValue"] = myNewValue; I am doing this because I need to mock the functionality of a class my app uses. Does anyone know how to do this with MOQ? I've tried variations on the following: Dictionary<string, object> MyContainer = new Dictionary<string, object>(); mock.ExpectGet<object>( p => p[It.IsAny<string>()]).Returns(MyContainer[(string s)]); But that doesn't compile. Is what I am trying