rhino-mocks

How to write unit test cases for async methods?

荒凉一梦 提交于 2019-12-07 18:16:53
问题 I would like to write a unit test case by mocking the dependencies. The overall flow is as follows. We have a WorklistLoader which has an async method LoadWorklistItemsAsync() . To accomplish this task WorklistLoader is dependent on lower layer API(which i want to mock) QueryManager.StartQueryTask() . StartQueryTask() is also a async method which queries the file system and raises the ProgressChanged() at regular intervals and then at the end raises the CompletedEvent . StartQueryTask()

Mocking tools for C# on .NET [closed]

好久不见. 提交于 2019-12-07 09:46:25
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I'm new to C#/.NET but I've been doing TDD for quite some time now. I want to know what is the best framework that can be used for mocking objects while writing tests in C#? 回答1: Popular options Rhino Moq TypeMock Isolator Moq and Rhino are both regular mock/stub framework. TypeMock Isolator is a bit special, as

Unit Testing error “Object reference not set to an instance of an object.”

霸气de小男生 提交于 2019-12-07 04:53:14
问题 In my controller I want to test if the controller is calling the repository method. Here is the method in controller [HttpGet] public ActionResult GetModulePropertyName(string moduleTypeValue) { var temp = _modulerepository.GetModuleKindPropertyNames(moduleTypeValue); IList<Property> model = temp .Select(item => new Property {Name = item}) .ToList(); return PartialView("GetModulePropertyName",model); } And here is the test method [TestMethod] public void GetModulePropertyName_Action_Calls

Rhino - Mocking classes and not overriding virtual methods

独自空忆成欢 提交于 2019-12-07 04:45:17
问题 If I'm mocking a class, like below, is there any way I can get the mock to not override a virtual method? I know I can simply remove the virtual modifier, but I actually want to stub out behavior for this method later. In other words, how can I get this test to pass, other than removing the virtual modifier: namespace Sandbox { public class classToMock { public int IntProperty { get; set; } public virtual void DoIt() { IntProperty = 1; } } public class Foo { static void Main(string[] args) {

What is the purpose of unit testing an interface repository

南楼画角 提交于 2019-12-07 02:17:02
问题 I am unit testing an ICustomerRepository interface used for retrieving objects of type Customer . As a unit test what value am I gaining by testing the ICustomerRepository in this manner? Under what conditions would the below test fail? For tests of this nature is it advisable to do tests that I know should fail? i.e. look for id 4 when I know I've only placed 5 in the repository I am probably missing something obvious but it seems the integration tests of the class that implements

How to mock HttpContext.Current.Items with NUnit and Rhino Mocks

孤街浪徒 提交于 2019-12-06 22:14:33
I'm using NUnit and RhinoMocks for unit testing on the (WebApi) project. There is a method I'm trying to write test for, which is supposed to add an item to HttpContext.Current.Items. public override void OnActionExecuting(HttpActionContext actionContext) { HttpContext.Current.Items.Add("RequestGUID", Guid.NewGuid()); base.OnActionExecuting(actionContext); } I have no idea how can I make HttpContext.Current.Items available to the method when ran from within a test method. How can I achieve this? Also, how can I check if the item has been added (what kind of assertion can/should I use) You don

Rhino Mocks - Using Arg.Matches

冷暖自知 提交于 2019-12-06 16:58:43
问题 I have a function I am mocking which takes an argument object as a parameter. I want to return a result based on the values in the object. I cannot compare the objects as Equals is not overriden. I have the following code: _tourDal.Stub(x => x.GetById(Arg<TourGet>.Matches(y => y.TourId == 2), null)).Return( new Tour() { TourId = 2, DepartureLocation = new IataInfo() { IataId = 2 }, ArrivalLocation = new IataInfo() { IataId = 3 } }); This should return the object specified when the supplied

How to mock a method (custom behavior) with Rhino Mocks in VB.NET

你离开我真会死。 提交于 2019-12-06 14:01:45
How can I mock one method with RhinoMocks in VB.Net? The reference I found is in C#: Expect.Call(delegate{list.Add(0);}).IgnoreArguments() .Do((Action<int>)delegate(int item) { if (item < 0) throw new ArgumentOutOfRangeException(); }); SharpDevelop converts this to: Expect.Call(Function() Do list.Add(0) End Function).IgnoreArguments().Do(DirectCast(Function(item As Integer) Do If item < 0 Then Throw New ArgumentOutOfRangeException() End If End Function, Action(Of Integer))) But that doesn't work either (it doesn't compile). This is what I want to do: create a new object and call a method which

Extension methods in Mono 2.4 and RhinoMocks 3.5

浪子不回头ぞ 提交于 2019-12-06 10:23:28
I am playing around with MonoDevelop 2.0 and Mono 2.4 in Ubuntu. I have run into problems with extension methods not being available (eg mockView.Stub(...)) in RhinoMocks 3.5 for AAA style tests. I downloaded the RhinoMocks dll from Ayende's site rather than compiled from source. My project in MonoDevelop is setup to target framework 3.5 Using the RhinoMocks c#2.0 syntax with static methods on the RhinoMocksExtensions class works. (e.g RhinoMocksExtensions.Stub(authSvc, delegate(IAuthService svc)) Should AAA syntax (and RhinoMocks in general) work with Mono 2.4 or is it likely I've not setup

Using RhinoMocks, how can I assert that one of several methods was called?

僤鯓⒐⒋嵵緔 提交于 2019-12-06 09:21:57
Consider the following service interfaces: public interface IServiceA { void DoSomething(string s); void DoSomething(string s, bool b); } public interface IServiceB { void DoSomething(); } The implementation of IServiceB depends on IServiceA like this: public class ServiceB : IServiceB { private IServiceA _serviceA; public ServiceB(IServiceA serviceA) { _serviceA = serviceA; } public void DoSomething() { _serviceA.DoSomething("Hello", true); } } Ie. the dependency is injected in the constructor. Now consider a unit test for the DoSomething() method. I wish to assert that one of the overloaded