moq

How to verify method arguments after method call?

Say I have a method A.Do(Arg arg) which assigns some properties of arg (class Arg), let say it sets arg.Prop1 = "done". And I'm testing a void method B.Do(void): public class B { public void Do() { var arg = InitArg(); A.Do(arg) ... } } and I've mocked class A as new Mock< A>() with CodeBase=true. So how do I verify...

Unit Test Service which has WCF Call (MSUnit + Moq)

Hi there, I am new to Mocking and somewhat familiar with unit testing and finally have decided to bite the bullet on a new project starting up front with a strict TDD approach. However I have one service class and method I need to retrospectively add tests to as it has been promoted from a prototype. I do not know where to start thoug...

Help with this unit test using Moq

I have the following so far which I am trying to unit test: private Mock<IDBFactory> _mockDbFactory; private IArticleManager _articleManager; [Setup] public Setup() { _mockDbFactory = new Mock<IDBFactory>(); _articleManager = new ArticleManager(_mockDbFactory); } [Test] public void load_article_by_title() { string t...

For me to use Moq, do all my classes have to implement an interface?

I want to use Moq, but I am using Nhibernate and I didn't create interfaces for all my Model classes (POCO classes). Do I have to create an interface for each class for me to be able to moq my POCO classes? ...

Moq: Invalid setup on a non-overridable member: x => x.GetByTitle("asdf")

Not sure how I can fix this, trying to do a unit test on the method "GetByTitle" Here are my definitions: public class ArticleDAO : GenericNHibernateDAO(IArticle, int>, IArticleDAO { public IArticle GetByTitle(string title) { IQuery query = Session.CreateQuery("...") return qu...

Unit test using Moq doesn't pass, object is null, did I miss something?

The class I want to test is my ArticleManager class, specifically the LoadArticle method: public class ArticleManager : IArticleManager { private IArticle _article; public ArticleManger(IDBFactory dbFactory) { _dbFactory = dbFactory; } public void LoadArticle(string title) { ...

Can you mock an object that implements an interface AND an abstract class?

Is it possible to use Moq to mock an object that implements an interface and abstract class? I.e.: public class MyClass: SomeAbstractClass, IMyClass Can you mock this? ...

to mock an object, does it have to be either implementing an interface or marked virtual?

or can the class be implementing an abstract class also? ...

Moq - verifying a method was called

Using Moq, I have a very odd issue where the setup on a mock only seems to work if the method I am setting up is public. I don't know if this is a Moq bug or if I just have this wrong (newbie to Moq). Here is the test case: public class TestClass { public string Say() { return Hello(); } inte...

Setting expectations on MoQ

Hi! I´m using MoQ to test some controllers I have but i'm not being able to set the expectations. This is the code I have: var rep = new Mock<IUserRepository>(); rep.Setup(r => r.Save()); The problem is that, my Save() method expects a User object which I cannot set in the expectation because a instance of it will be ...

Why doesn't Moq run the overridden ToString method?

In the following code why does mockTest.ToString() return Null? EDIT: Added comment into example code to show how to fix the problem. Public Sub Main() Try Dim test = New TestClass If test.ToString <> "stackoverflow rules" Then Throw New Exception("Real Failed: Actual value: <" + test.ToString + ">") ...

Unit testing domain objects

We have a requirement to add an event reminder when a user enters their email address on an event page. Event is another domain object. Our initial thought was to create a Customer domain object and related CustomerService: public class CustomerService { public void AddEventReminder(string emailAddress, int eventId) { var cus...

Mocking webservice calls...sometimes

I am trying to write acceptance testing for an existing app. I've run into a problem though when calling a web service that tells us if a person is, in short, in the office or not, what hours, and who the backup is. In most of the tests, actually calling the web service is fine... yes, ideally it shouldn't, but creating inputs and outp...

Moq and accessing called parameters

I've just started to implement unit tests (using xUnit and Moq) on an already established project of mine. The project extensively uses dependency injection via the unity container. I have two services A and B. Service A is the one being tested in this case. Service A calls B and gives it a delegate to an internal function. This 'callb...

using Moq and NUnit , what is write approach / better syntax ?

I am trying to test this behavior -- BLOGTableAdapter.GetBlogsByTitle(string title) is called and for once only -- and is called with string having length greater than 1, -- and it returns BLOGDataTable Object [Test] public void GetBlogsByBlogTitleTest4() { var mockAdapter = new Mock<BLOGTableAdapter>(); ...

c# Mocking Interface members of a concrete class with Moq

I have an interface ITransaction as follows: public interface ITransaction { DateTime EntryTime { get; } DateTime ExitTime { get; } } and I have a derived class PaymentTransaction as follows: public class PaymentTransaction : ITransaction { public virtual DateTime LastPaymentTime { get {...

How to mock an object, declared / initialized inside a method under test ?

Consider this programs public class ItemManager { private ItemFetcher itemFetcher; public ItemManager(ItemFetcher _itemFetcher) { itemFetcher = _itemFetcher; } public List<Item> GetItemsFomTable() { List<Item> itemsList = new List<Item>(); Item item; DataTable resultDataTable = it...

IOC on IValidationDictionary with Castle Windsor

I'm new to Castle Windsor and am just using the latest version. I've created entries for my repositories which are working fine but I have one final dependency that I'm passing into my controller. I've created a ModelStateWrapper which inherits from IValidationDictionary. The ModelStateWrapper takes a ModelStateDictionary in it's constr...

Moq and Command Pattern .I am struggling can you help?

Hi all, New to the world of TDD and I have soon find out that mocking at times is not as easy. We are using MOQ at work so I need to learn how to do this using moq I have some code using the command pattern and works a treat.However If were to test drive it I would not know how to do it implementing the code below. I have done the foll...

asp.net mvc: how to mock Url.Content("~") ?

anybody knows how to mock Url.Content("~") ? (I'm using Moq) ...