moq

What are the differences between MOQ and AutoFixture?

喜夏-厌秋 提交于 2019-12-05 16:43:12
问题 I have a fair amount of experience using MOQ, while I've recently have stumbled into AutoFixture. What are the differences between these frameworks? 回答1: The FAQ explains the difference. In short AutoFixture uses Reflection to create 'well-behaved' instances of public types. It auto-generates instances of other types if necessary to fill in arguments for a constructor, and also assigns values to public writable properties. In essence, it simply uses the requested type's public API to

How do I mock HttpResponseBase.End()?

陌路散爱 提交于 2019-12-05 15:53:05
I'm using Moq to create a mock object of HttpResponseBase. I need to be able to test that HttpResponseBase.End() was called in my library. To do this, I specify some text before the call and some text after. Then I check that only the text before the call to End() is present in HttpResponseBase.Output. The problem is, I can't figure out how to mock HttpResponseBase.End() so that it stops processing, like it does in ASP.NET. public static HttpResponseBase CreateHttpResponseBase() { var mock = new Mock<HttpResponseBase>(); StringWriter output = new StringWriter(); mock.SetupProperty(x => x

How can I mock ServiceStack IHttpRequest

无人久伴 提交于 2019-12-05 15:16:48
I'm trying to get a unit test working for a service that is injecting items into the IHttpRequest.Items, using a request filter: this.RequestFilters.Add((req, res, dto) => { // simplified for readability... var repo = container.Resolve<IClientRepository>(); var apiKey = req.Headers["ApiKey"]; // lookup account code from api key var accountcode = repo.GetByApiKey(apiKey); req.Items.Add("AccountCode", accountCode); }); My service uses that dictionary item: public class UserService : AppServiceBase { public IUserServiceGateway UserServiceGateway { get; set; } public object Any(UserRequest request

What is needed in the HttpContext to allow FormsAuthentication.SignOut() to execute?

孤街醉人 提交于 2019-12-05 14:54:38
问题 I am trying to write a unit test for our log out method. Among other things it FormsAuthentication.SignOut() . However, it throws a System.NullReferenceException . I've created a mock; HttpContext (using Moq), but it is obviously missing something. My mock context contains: A mocked HttpRequestBase on Request A mocked HttpResponseBase on Response With a HttpCookieCollection on Request.Cookies and another on Response.Cookies A mocked IPrincipal on User I am aware I could go the wrapper route

Mocking methods with Expression<Func<T,bool>> parameter using Moq

隐身守侯 提交于 2019-12-05 13:38:15
I want to mock this interface using Moq IInterfaceToBeMocked { IEnumerable<Dude> SearchDudeByFilter(Expression<Func<Dude,bool>> filter); } I was thinking of doing something like _mock.Setup(method => method.SearchDudeByFilter( x=> x.DudeId.Equals(10) && X.Ride.Equals("Harley"))). Returns(_dudes);// _dudes is an in-memory list of dudes. When I try to debug the unit test where i need this mocking, it says that "expression is not allowed" pointing towards the lambda. If it makes any difference I am using xUnit as the testing framework. The following works fine for me with the Moq 4.0 Beta :

How to test ServiceStack Service using Moq

♀尐吖头ヾ 提交于 2019-12-05 12:37:13
I have a rest service that I have created with ServiceStack, using nHibernate as a way of getting the data from a SqlCe database. I've been trying to write some unit tests using nUnit and Moq - I have successfully mocked the nHibernate implementation to return null, invalid objects and so on - but my tests always throw a NullReferenceException when it calls the base class to set the HttpStatus etc. public List <ParameterDto> Get (ParameterAll parameterAll) { List <ParameterDto> parameterResponseList = new List <ParameterDto> (); try { List <ParameterDomainObject> parameterDomainObjectList =

Mocking Delegate.Invoke() using Moq throws InvalidCast exception in LINQ

若如初见. 提交于 2019-12-05 12:05:52
问题 Let's say that I have IService interface: public interface IService { string Name { get; set; } } And a delegate Func<IService> that returns this interface. In my unit test I want to mock the delegate's Invoke() method using Moq like this: [TestMethod] public void UnitTest() { var mockService = new Mock<IService>(); var mockDelegate = new Mock<Func<IService>>(); mockDelegate.Setup(x => x.Invoke()).Returns(mockService.Object); // The rest of the test } Unfortunately mockDelegate.Setup(...)

Can mock objects setup to return two desired results?

冷暖自知 提交于 2019-12-05 11:27:54
Can mock objects be used to return more than one desired result like below? mockObject.Setup(o => o.foo(It.IsAny<List<string>>())).Returns(fooBall); mockObject.Setup(o => o.foo(It.IsAny<int>())).Returns(fooSquare); Yes, you can use these setups. Thus arguments of foo method call are different (any integer and any list of strings), you have two different setups here, each with its own return value. If you would have same arguments, then last setup would replace previous setup(s). Remember - each setup is defined by member you call and arguments you pass. In your example, because the two foo

Moq: Invalid callback. Setup on method with parameters cannot invoke callback with parameters

℡╲_俬逩灬. 提交于 2019-12-05 10:40:23
问题 I am trying to use moq to write a unit test. Here is my unit test code var sender = new Mock<ICommandSender>(); sender.Setup(m => m.SendCommand(It.IsAny<MyCommand>(), false)) .Callback(delegate(object o) { var msg = o as MyCommand; Assert.AreEqual(cmd.Id, msg.Id); Assert.AreEqual(cmd.Name, msg.Name); }) .Verifiable(); SendCommand takes an object and optional boolean parameter. And MyCommand derives from ICommand. void SendCommand(ICommand commands, bool idFromContent = false); When the test

c# mocking IFormFile CopyToAsync() method

徘徊边缘 提交于 2019-12-05 10:11:58
I am working on a unit test for an async function that converts a list of IFormFile to a list of my own arbitrary database file classes. The method that converts the file data to a byte array is: internal async Task<List<File>> ConvertFormFilesToFiles(ICollection<IFormFile> formFiles) { var file = new File { InsertDateTime = DateTime.Now, LastChangeDateTime = DateTime.Now }; if (formFile.Length > 0) { using (var memoryStream = new MemoryStream()) { await formFile.CopyToAsync(memoryStream, CancellationToken.None); file.FileData = memoryStream.ToArray(); } } // ... } The function receives an