moq

Mocking 3rd party callback events using moq

六月ゝ 毕业季﹏ 提交于 2019-12-03 07:13:37
We've been trying write unit tests for a worker class written in C#, which mocks out a third party API (COM based) using moq to dynamically create the mock objects. NUnit is our unit testing framework. This third party component implements a couple of interfaces, but also needs to call back into our worker class using events. Our plan was to simulate the events that this 3rd party component can raise, and test that our worker class operated as expected. Unfortunately we've run into a problem in that moq seems unable to mock out and raise events that are defined externally. Unfortunately I can

Moq (or possibly another framework) on Mono / MonoTouch

六月ゝ 毕业季﹏ 提交于 2019-12-03 06:57:35
I've just started some MonoTouch development and I've tried, and failed, to get Moq working for my unit tests. The binary version fails because it's looking for System v2.0, which I assume is down to its Castle requirements, and building it from source crashes the compiler! My question is has anyone gotten Moq to work on Mono (the touch part should be irrelevant, I'm not deploying it to the phone!), or had any joy with any of the other mocking frameworks? Failing that I'm back to rolling my own, which is a bit of a pain. I'm using Moq right now with Monodevelop to test the non-Monotouch parts

Unit Testing Mock/Stub definitions in Moq

人走茶凉 提交于 2019-12-03 06:32:44
Any reading or advice I've been given on Unit Testing has always suggested a distinct difference between the definition of a Mock and a Stub. My current understanding of these definitions are as follows Mock: A fake which will be used in your test to make a final assertion Stub: A fake which will be used in your test to isolate a dependency but not be asserted However, Moq appears to only allow the creation of Mocks. The Stub namespace in the framework appears to be depreciated with recommendations to use Mock.SetupXXX. Am I missing something in my understanding of this? Or is there a general

What is AutoFixture AutoMoq?

旧时模样 提交于 2019-12-03 06:29:04
问题 I was looking at nuget and wanted to import moq when I noticed AutoFixture AutoMoq. I see that AutoFixture is to help write TDD faster but I can't find any examples of AutoMoq and how it is different then AutoFixture. Can someone point me to this AutoMoq so I can see what it is doing. 回答1: In short, AutoFixture.AutoMoq is an extension that turns AutoFixture into an Auto-Mocking Container using the Moq dynamic mock library. There's also a similar extension for AutoFixture that enables auto

How can I use Mock Objects in my unit tests and still use Code Coverage?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 06:06:24
Presently I'm starting to introduce the concept of Mock objects into my Unit Tests. In particular I'm using the Moq framework. However, one of the things I've noticed is that suddenly the classes I'm testing using this framework are showing code coverage of 0%. Now I understand that since I'm just mocking the class, its not running the actual class itself....but how do I write these tests and have Code Coverage return accurate results? Do I have to write one set of tests that use Mocks and one set to instantiate the class directly. Perhaps I am doing something wrong without realizing it? Here

How to moq a static class with a static method (UnitOfWork case)?

眉间皱痕 提交于 2019-12-03 06:03:22
I have these classes: public static class UnitOfWorkSS { public static IUnitOfWork Begin() { return IoC.Resolve<IUnitOfWork>(); } } public class PostService { using (IUnitOfWork unitOfWork = UnitOfWorkSS.Begin()) { //don't forget to sanitize html content htmlContent = _htmlSanitizer.Sanitize(htmlContent); IPost post = _factory.CreatePost(byUser, title, htmlContent); _postRepository.Add(post); unitOfWork.Commit(); } } How can I mock the classes UnitOfWorkSS and unitOfWork ? Josh It looks like the only thing you are doing with the call to Begin() is returning your configured class for that

How do I mock controller context in my unit test so that my partial view to string function works?

孤街醉人 提交于 2019-12-03 05:30:25
I am attempting to create a unit test for my controller, but the action I am testing uses a partial view to string function which doesn't want to work in my tests. private string RenderPartialViewToString(string viewName, object model = null) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.RouteData.GetRequiredString("action"); ViewData.Model = model; using (System.IO.StringWriter sw = new System.IO.StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); ViewContext viewContext = new ViewContext(ControllerContext,

multiple parameters call back in Moq

做~自己de王妃 提交于 2019-12-03 05:28:54
Can someone please look at the code below and see what's wrong? [TestInitialize] public void SetupMockRepository() { var memberId = "34345235435354545345"; var title = "test"; var url = "dafdsfdsfdsfdsafd"; _mockPropertySearchRepository = new Mock<IPropertySearchRepository>(MockBehavior.Strict); _mockPropertySearchRepository.Setup(p => p.SaveSearchURL(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Callback<string,string,string>((id,t,u) => ).Returns(new SavedSearchReturnResult() ); } Thanks user2388013 I managed to solve the problem myself as below [TestInitialize] public void

Moq: Specifying return values as part of expectations

旧时模样 提交于 2019-12-03 05:07:34
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>(); customerMock.Setup(c => c.DoSomething("Jo", "Blog", 1)).Returns("OK"); customerMock.Verify(c => c

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

走远了吗. 提交于 2019-12-03 04:58:38
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 wouldn't even allow it....So then is there a time where it is better to mock a class over an interface