moq

Using Moq callbacks correctly according to AAA

I've created a unit test that tests interactions on my ViewModel class in a Silverlight application. To be able to do this test, I'm mocking the service interface, injected to the ViewModel. I'm using Moq framework to do the mocking. to be able to verify bounded object in the ViewModel is converted properly, I've used a callback: [T...

When can we mock an object and its methods?

I am novice to the Moq and unit testing. I have to write unit tests to a lot of classes which has the objects of other classes. can i mock the methods of the class objects. Here is the exact scenerio- I have a class two classes A and B and A has a private object of B and in a method of A i am internally calling the method of B and then ...

Creating a Mock DetactedCreteria Object

I have a function in my service layer whose output needs to be mocked. it takes in a FinanceAccount object and returns a List the function uses the FinanceAccount object to create a DetachtedCreteria. and fetch a List. How can I SetUp this function so that it returns separate Lists for 3 three different FinanceAccount objects. I am usi...

When mocking a class with Moq, how can I CallBase for just specific methods?

I really appreciate Moq's Loose mocking behaviour that returns default values when no expectations are set. It's convenient and saves me code, and it also acts as a safety measure: dependencies won't get unintentionally called during the unit test (as long as they are virtual). However, I'm confused about how to keep these benefits when...

Moq Callback can you help to understand it.

Hi Using moq and looked at callback but I have not been able to find a simple example to understand how to use it. Do you have a small working snippet which clearly explain how and when to use it? Thanks a lot ...

Moq how to correctly mock set only properties

What is the correct way for dealing with interfaces the expose set-only properties with Moq? Previously I've added the other accessor but this has bled into my domain too far with random throw new NotImplementedException() statements throughout. I just want to do something simple like: mock.VerifySet(view => view.SetOnlyValue, Times.Ne...

Mocking a Wcf ServiceContract

I want to mock a ServiceContract. The problem is that Moq (and Castle Dynamic-Proxy) copies the attributes from the interface to the dynamic proxy which Wcf don't like. Wcf sais: The ServiceContractAttribute should only be define on either the interface or the implementation, not both. Exception is: InvalidOperationException - The servi...

Do we really need isolation frameworks to create stubs?

I have read this: http://martinfowler.com/articles/mocksArentStubs.html My concepts about a stub and a mock are clear. I understand the need of isolation frameworks like moq, rhinomocks and like to create a mock object. As mocks, participate in actual verfication of expectations. But why do we need these frameworks to create stubs. I wou...

Moq basic questions

I made the following test for my class: var mock = new Mock<IRandomNumberGenerator>(); mock.Setup(framework => framework.Generate(0, 50)) .Returns(7.0); var rnac = new RandomNumberAverageCounter(mock.Object, 1, 100); rnac.Run(); double result = rnac.GetAverage(); Assert.AreEqual(result, 7.0, 0.1); The problem here was that I chan...

Can I mock Objectresult<T> of Entity Framework Using MOQ

I am using Entity Frameowrk 4.0 and I am calling a stored procedure which returns an ObjectResult and I tried to use MOQ and have not been able to mock ObjectResult. Has anybody been able to mock ObjectResult using moq? TIA Yaz ...

Using Moq to set indexers in C#

I'm having trouble figuring out how to set indexers in C# with Moq. The Moq documentation is weak, and I've done a lot of searching... what I'd like to do is similar in the solution to How to Moq Setting an Indexed property: var someClass = new Mock<ISomeClass>(); someClass.SetupSet(o => o.SomeIndexedProperty[3] = 25); I want to modif...

[NUnit+Moq] Guidelines for using Assert versus Verify

I'm new to unit testing, and I'm learning how to use NUnit and Moq. NUnit provides Assert syntax for testing conditions in my unit tests, while Moq provides some Verify functions. To some extent these seem to provide the same functionality. How do I know when it's more appropriate to use Assert or Verify? Maybe Assert is better for con...

Intelligent serial port mocks with Moq

I have to write a lot of code that deals with serial ports. Usually there will be a device connected at the other end of the wire and I usually create my own mocks to simulate their behavior. I'm starting to look at Moq to help with my unit tests. It's pretty simple to use it when you need just a stub, but I want to know if it is possib...

moq SetupSet is obsolete. In place of what?

Let's say I want to use moq to create a callback on a setter to store the set property in my own field for later use. (Contrived example - but it gets to the point of the question.) I could do something like this: myMock.SetupSet(x => x.MyProperty).Callback(value => myLocalVariable = value); And that works just fine. However, Setup...

Mocking a datacontext for an object that has a dependancy

Hey everyone, I'm writing some unit tests in my project and I have a datacontext dependancy on the controller containing the methods I'd like to test. I'm using Ninject to inject the dependancy and Moq to create my mock datacontext. My DI makes use of an interface IDataContext which my dbml impliments and is used through out the inject...

Use Moq to mock Constructor?

I have such a set of Constructors: public BusinessObjectContext() : this(CloudStorageAccount.FromConfigurationSetting("DataConnectionString").TableEndpoint.ToString(), CloudStorageAccount.FromConfigurationSetting("DataConnectionString").Credentials) {} public BusinessObjectContext(string dataConnectionString) : ...

Moq and the parameters you pass

I create a MOQ object pass it to my class call it and pass in an object. How can I get access to this object to see if the right properties are set? ...

Testing and mocking private/protected methods. Many posts but still cannot make one example work

Hi, I have seen many posts and questions about "Mocking a private method" but still cannot make it work and not found a real answer. Lets forget the code smell and you should not do it etc.... From what I understand I have done the following: 1) Created a class Library "MyMoqSamples" 2) Added a ref to Moq and NUnit 3) Edited the Asse...

Moq for Silverlight doesn't raise event

Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void DataReceived(ObservableCollection<TeamPlayerData> AllReadyPlayers, GetSquadDataCompletedEventArgs squadDetails); public interface ISquadModel : IModelBase { void RequestData(int matchId, int teamId); void SaveData(); event DataReceived...

Moq file version & assembly version mismatch, causing problems in TeamCity?

Setting up TeamCity 5.0.1 to run unit tests for the first time, we're seeing this error: Test(s) failed. System.IO.FileNotFoundException : Could not load file or assembly 'Moq, Version=3.1.416.3, Culture=neutral, PublicKeyToken=69f491c39445e920' or one of its dependencies. The system cannot find the file specified. We're using vers...