nmock

What's a good way to write Unit tests for code with protected objects C# (using NMock, and NUnit framework)

When writeing unit tests for a single class that contains other objects what's the best way to use mock objects to avoid tests dependant on other classes. Example 1: public class MyClass { protected MyObject _obj; public MyClass() { _obj = new MyObject(); } public object DoSomething() { //some work ...

Unable to use nMock GetProperty routine on a property of an inherited object...

I am getting this error when trying to set an expectation on an object I mocked that inherits from MembershipUser: ContactRepositoryTests.UpdateTest : FailedSystem.InvalidProgramException: JIT Compiler encountered an internal limitation. Server stack trace: at MockObjectType1.ToString() Exception rethrown at [0]: at System.Runtime....

C# Noob - Triggering event in mocked interface class - How does this code work?

I'm a little confused at what's going on here. I'm looking at the Puzzle example from Atomic Object showing how to test a Model-View-Presenter pattern Puzzle.zip The View has a private event. The view also has a Subscribe(delegate) function that adds the delegate to the event. The Presenter is passed in an IView and an IModel. Duri...

Using NMock with ByRef parameters

I have to work with an API that uses a lot of by-reference parameters. I'm just beginning to use NMock, and I'm having trouble seeing how to make NMock actually modify one of those by-ref parameters to a specific value. Am I missing something, or does it just not do that? Is there a better way, short of wrapping this API? ...

When to Expect and When to Stub?

I use NMock2, and I've drafted the following NMock classes to represent some common mock framework concepts: Expect: this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call to VerifyAllExpectationsHaveBeenMet()). Stub: this specifies what a mocked method should r...

How to mock GetType() method in NMock

Hi all, I have some production code like private bool IsTypeEqual(object theFirstObject, object theSecondObject) { if(theFirstObject.GetType()==theSecondObject.GetType()) { return true; } else { return false; } } Now i have to write the unit test case for this c...

Is there a way to unit test an async method?

I am using Xunit and NMock on .NET platform. I am testing a presentation model where a method is asynchronous. The method creates an async task and executes it so the method returns immediately and the state I need to check aren't ready yet. I can set a flag upon finish without modifying the SUT but that would mean I would have to keep ...

Basic NMock database examples for CRUD application

I'm looking for some basic examples of using NMock2 to mock database calls for a CRUD application. Thanks, Chris ...

NMock Problem with Lists

Hi, I have the following test using NMock which fails. It used to work when the result from the service call was passed to the view but it now fails since the results are converted to dto's. I think this might mean I need to create a custom matcher but I am not sure. Does anyone have any ideas? Error Message: Test method Dgc.Cpo.RM.U...

NMock - how to say expect 'any value' on these params? Or should I not do this?

Hi, I have a quick question that I could not figure out in the docs about NMock2.0. I have a function called Save() that I want to mock out. This takes a string ID as parameter and a decimal as value.. I know I can write this to make sure that Save() gets called with 2 specific values: Expect.Once.On(dao) _ .Method("Save").Wi...

Differences between NMock 2.0 and NMock2

I am a bit confused over which version of NMock2 I should use. The one I've been using for a while I got from here: http://www.nmock.org/download.html The filename is NMock2.dll with version 2.0.0.44. I ran into a problem where I couldn't mock interfaces containing generic functions. After a quick google search found out that NMock2 s...

Can NMock expect method invocations in background threads?

I'm creating a WPF application using the Composite Application Library (CAL), aka PRISM. I'm also using MVVM (Model-View-ViewModel). To prevent background processing from blocking the UI and making it unresponsive, I want to have the view model make most work calls on a background thread. To do this, I'm using the .NET ThreadPool object...

Mocking help?I can't find what's the problem

Hi, I have an interface definded like this: public interface IDatabase{ void Get<TTypeToFetch> ();} and when I try to do: Mockery mockery = new Mockery(); IDatabase db = mockery.NewMock<IDatabase>(); I get the following error: System.TypeLoadException: System.TypeLoadException: Signature of the body and declaration in a method...

NMock2.0 - how to stub a non interface call?

Hello, I have a class API which has full code coverage and uses DI to mock out all the logic in the main class function (Job.Run) which does all the work. I found a bug in production where we werent doing some validation on one of the data input fields. So, I added a stub function called ValidateFoo()... Wrote a unit test against this...

How to assert a private method on concrete class is called (TypeMock/NMock/etc..)?

Hello, I am trying to write a unit test for the 'IsUnique' function in the class below that looks like this: class Foo { public bool IsUnique(params...) { ValidateStuffExists(params); return CheckUniqueness(params); } private void ValidateStuffExists(params) { //does some validation } p...

Using nMoq, how would one expect for a given Event?

Let's say I want to make a Unit-Test where I have this Tetris game and I want to start the game, do nothing, and wait for the game to be over (this is, to get a GameOver event): Tetris tetris = new Tetris(); tetris.GameOver += something; tetris.Start(); How should I make the test? This should probably be easy but I can't see how to do...

How can I mock an internal interface using NMock2?

If I try this, I just get an exception: System.TypeLoadException : Access is denied: 'Namespace.IInternalInterface'. Making the interface public is not an acceptable solution. I don't want to change the visiblity of my API in order to test it. ...

Moq: How to mock a generic method (when the Generic Type T is internal) ?

I've the following method defined public interface IData { T GetUserById<T>(int id) where T : IMyUser, new(); } The actual use of this method is like : da.GetUserById<MyUser>(id); where the MyUser is an internal class defined in the business logic and cannot be used by the unittest. In NMock this can be done using this code: ...