moq

ASP.NET MVC Custom Route Constraints, Dependency Injection and Unit Testing

随声附和 提交于 2019-12-03 15:56:14
About this topic, I have asked another question: ASP.NET MVC Custom Route Constraints and Dependency Injection Here is the current situation: on my ASP.NET MVC 3 App, I have a route constraint defined like below: public class CountryRouteConstraint : IRouteConstraint { private readonly ICountryRepository<Country> _countryRepo; public CountryRouteConstraint(ICountryRepository<Country> countryRepo) { _countryRepo = countryRepo; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { //do the database look

Moq: Specifying return values as part of expectations

孤人 提交于 2019-12-03 15:26:13
问题 I'm new to Moq and learning. I need to test that a method returns the value expected. I have put together a noddy example to explain my problem. This fails miserably with: "ArgumentException: Expression is not a method invocation: c => (c.DoSomething("Jo", "Blog", 1) = "OK")" Can you correct what I am doing wrong? [TestFixtureAttribute, CategoryAttribute("Customer")] public class Can_test_a_customer { [TestAttribute] public void Can_do_something() { var customerMock = new Mock<ICustomer>();

What is the difference between Moq-ing a class or interface?

拥有回忆 提交于 2019-12-03 15:17:44
问题 I've been using moq to mock objects in my unit tests and I've seen on the site about moq that it is able to mock both classes and interfaces. I had a discussion with one of my work mates the other day and they stated that there is never a reason to mock classes and I should only mock interfaces. I didn't really have an answer to that....and I can't seem to find any answer to that on the moq site either. Is it true that one should never mock classes? I'd say no since if that were true then Moq

ASP.NET MVC Unit Testing - Sessions

坚强是说给别人听的谎言 提交于 2019-12-03 15:13:54
问题 Having searched StackOverflow, and Google I think what I'm doing is suppose to be right, however results don't seem to be going well [TestMethod] public void LoginAction_Should_Return_View_and_User_Authenticated() { // Arrange var mock = new Mock<ControllerContext>(); var mockSession = new Mock<HttpSessionStateBase>(); mock.Setup(p => p.HttpContext.Session).Returns(mockSession.Object); var testData = FakeUserData.CreateTestUsers(); var repository = new FakeUserRepository(testData); var

Mock.Of<Object> VS Mock<Object>()

喜欢而已 提交于 2019-12-03 14:42:48
问题 I'm currently confuse on how to mock. I'm using Moq. To mock objects I usually write this way var mockIRepo = new Mock<IRepo>(); However, I need to create mock object for my setup. Option1 Is it better to mock my object which only contain properties this way? var object = Mock.Of<Object>() Option2 Or this way var object = new Mock<Object>() I've read that option 2 has setupproperties which is kinda questionable to me because I could also set the properties in option 1. Then what is the

How do you use Moq to mock a simple interface?

廉价感情. 提交于 2019-12-03 14:31:19
问题 Okay, I have a business logic class like this: Note: For context, Vendor Briefs are simple entities that describe a "download" for a PDF document. /// <summary> /// Houses business level functions for dealing with vendor briefs. /// </summary> public class VendorBriefController : IVendorBriefController { /// <summary> /// Vendor brief controller requires an instance of IVendorBriefRepository. /// </summary> IVendorBriefRepository _vendorBriefRepository; /// <summary> /// Initializes an

Moq fake one method but use real implementation of another

怎甘沉沦 提交于 2019-12-03 14:25:29
问题 Given an interface IService that has Method1() and Method2() . I want to test that when Method1() throws an Exception , Method2( ) is called and returns a given value . ( Method2() is called when Method1() throws). Therefore I need to test a real Method2() with a fake Method1() , they are methods of the same interface. Here is my test code: MBase sut.MethodX() is the only entry point. It uses IService . My aim is to assert that Method2() returns something . // Arrange // Fake bytes in. var

How to mock a class that implements multiple interfaces

六眼飞鱼酱① 提交于 2019-12-03 14:19:46
问题 How to mock the following class: UserRepository : GenericRepository<User>, IUserRepository public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class I am using Moq, and I am confused how to handle multiple interfaces correctly. 回答1: Take a look at https://github.com/Moq/moq4/wiki/Quickstart Advanced Features // implementing multiple interfaces in mock var foo = new Mock<IFoo>(); var disposableFoo = foo.As<IDisposable>(); // now IFoo mock also implements

Passing Moq mock-objects to constructor

六月ゝ 毕业季﹏ 提交于 2019-12-03 14:19:46
问题 I've been using RhinoMocks for a good while, but just started looking into Moq. I have this very basic problem, and it surprises me that this doesn't fly right out of the box. Assume I have the following class definition: public class Foo { private IBar _bar; public Foo(IBar bar) { _bar = bar; } .. } Now I have a test where I need to Mock the IBar that send to Foo. In RhinoMocks I would simply do it like follows, and it would work just great: var mock = MockRepository.GenerateMock<IBar>();

How to mock RestSharp portable library in Unit Test

大憨熊 提交于 2019-12-03 13:51:30
问题 I would like to mockup the RestClient class for test purposes public class DataServices : IDataServices { private readonly IRestClient _restClient; public DataServices(IRestClient restClient) { _restClient = restClient; } public async Task<User> GetUserByUserName(string userName) { User user = null; // create a new request var restRequest = new RestRequest("User", Method.GET); // create REST parameters restRequest.AddParameter("userName", userName, ParameterType.QueryString); // execute the