Questions tagged [nmock]

NMock is a mocking framework for .NET 3.5 and 4.0. It supports lambda expressions for member matching. It has an easy to read syntax.

NMock is a dynamic mock object test library for .NET.

NMock2 is already available.

51 questions
23
votes
5 answers

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…
Jiho Han
  • 1,610
  • 1
  • 19
  • 41
14
votes
3 answers

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…
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
7
votes
3 answers

Test a public method which calls a private method using NUnit

I have a public method in a class that internally calls a particular private method within that class. It looks something like this : public class MyClass : IMyClassInterface { public List MyMethod(int a, int b) { …
Siddhant
  • 571
  • 1
  • 8
  • 32
5
votes
2 answers

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

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…
dferraro
  • 6,357
  • 11
  • 46
  • 69
5
votes
1 answer

c# how to get a stream processed by httpResponse.BinaryWrite

I have the following method that writes a stream in a HttpResponse object. public HttpResponse ShowPDF(Stream stream) { MemoryStream memoryStream = (MemoryStream) stream; httpResponse.Clear(); httpResponse.Buffer =…
betogrun
  • 93
  • 2
  • 7
4
votes
2 answers

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() { …
Jim
3
votes
2 answers

Unit test with nUnit and nMocks

Bit of a nUnit / nMock / Unit testing noobie question: I am trying to unit test this class. I have created a mock of it because I want to know the value returned from "getCurrencyRates", so that I can create tests based on that data. So I created a…
Positonic
  • 9,151
  • 14
  • 57
  • 84
3
votes
3 answers

Mocking a private field

I know a similar question has been asked but I have not found a clear solution. I'm trying to mock a private field from a large class. The private field gets instantiated in some earlier method and I'm trying to unit test a latter method which…
Samuel Heaney
  • 773
  • 1
  • 8
  • 14
2
votes
1 answer

NMock : redefine method expectation

I'm new to NMock and mocking in general. Is it possible to redefine an expectation ? In fact, I whant to mock an interface with many methods. So I decided to factorize common method expectations not to have to write them 1000 times. My issue is the…
boblemar
  • 1,143
  • 1
  • 10
  • 24
2
votes
1 answer

NMock issue testing against WPF and Dispatcher

Here's one for the threading junkies out there. I've got this method: public void RefreshMelts() { MeltsAvailable.Clear(); ThreadPool.QueueUserWorkItem(delegate { …
2
votes
1 answer

NMock - How to make a method with same argument types return different values?

I've a method like this: public string MyMethod(string a, string b) { if(a == "abcd" && b == "xyz") return "good"; if(a == "xyz" && b == "something") return "even better"; return "unexpected"; } public string MainMethod() …
AlterEgo
  • 81
  • 4
2
votes
1 answer

C# Mock a Class With an Internal Property Setter

I am trying to build a unit test. The class Position is implemented in a third party library. But for my unit test I need the Size property to be set to a specific value. public class Position { private double _size; private double Size …
Konstantin
  • 461
  • 1
  • 5
  • 13
2
votes
2 answers

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 +=…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
2
votes
2 answers

Mock objects being created inside a method while Unit Testing

I have a following scenario: public void DoSomething(...) { ... ClassA obj1 = new ClassA(); ClassB obj2 = new ClassB(); ClassC obj3 = new ClassC(); ... } I understand that if I was using Dependency Inversion Principle, I could Mock…
tunafish24
  • 2,288
  • 6
  • 28
  • 47
2
votes
1 answer

NMock3 How to Mock a method with out parameter?

I have a method that has one of its parameters as out, I found a reference on how to Mock it in NMock2. However, it seems the NMock3 has major changes that break the solution from NMock2. Here's the Interface signature for my method: …
Roman Mik
  • 3,179
  • 3
  • 27
  • 49
1
2 3 4