mocking

How to Mock (with Moq) Unity methods

Extension methods are not good for testing (that's described here: http://stackoverflow.com/questions/2295960/mocking-extension-methods-with-moq, http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx). But probably there are some solutions for mocking of Unity methods? In my case I have the followi...

How to mock IO/Network classes?

Hi all, I'm wondering how can I mock IO/Network classes. For example I have a class that do FTP operations or a class that writes into a file. How can I use mocking to mock and unit test such classes? I'm a C#/NHibernate/Castle ActiveRecord/Rhino Mocks developer. ...

Final method mocking

Hi! I need mock some class with final method using mockito. I have wrote something like this @Test public void test() { B b = mock(B.class); doReturn("bar called").when(b).bar(); assertEquals("must be \"overrided\"", "bar called", b.bar()); //bla-bla } class B { public final String bar() { return "fail"...

Mocking Javascript objects (specifically YUI)

Hi, I'm trying to mock YUI for javascript testing. I want to mock a datatable, but im not sure how to do it. Something like YAHOO.widget.DataTable = function(){ }; so latter I could do something like assert.that(returnedDatatable, isOfType(YAHOO.widget.DataTable)); ...

Partial Mocking As Code Smell?

Why is there so much hating going on about 'partial mocking' and the code that requires it? Here's an (theoretical) example implementation: public ComplexResult1 operationA(Stimulus a) { { ... result = ...; } auditTheChange(a); } public ComplexResult2 operationB(Stimulus b) { { ... result...

PHPUnit Test How Many Times A Function Is Called

I'm working on a test in phpunit and I'm running into an issue. I have a public function on my class that I am trying to test. Depending on the parameters passed in to the method, a protected function also in my test class will be called one or two times. I currently have a test in place to check that the return data is correct, but I...

How to mock chained function calls in python?

I'm using the mock library written by Michael Foord to help with my testing on a django application. I'd like to test that I'm setting up my query properly, but I don't think I need to actually hit the database, so I'm trying to mock out the query. I can mock out the first part of the query just fine, but I am not getting the results I...

Mocking Interface Events Error

Hello, I'm trying to mock an interface's events like the following: [TestMethod] public void NeedingDataFiresEvents() { //Arrange var service = MockRepository.GenerateMock<IService>(); service.Expect(i => i.GetValue()).Return(5); var view = MockRepository.GenerateMock<ILogView>(); view.NeedData += null; LastCall...

Java fake a Method given some Metadata from an MBean

I am trying to create a fake MBean server, so when it comes to evoking operations, I am running into problems. I have grabbed all the metadata from the mbean and put it into an xml file. I have the attributes working fine it seems, but having problems with the operations. I have all their metadata (method name, return type,etc) but I nee...

How does TypeMock mock sealed classes and non-virtual methods?

I don't want to know how to use TypeMock. I'm just intrigued as to how it manages to mock non-virtual methods and sealed or static classes. I'd like to try to do something similar - for the fun of it. How are the generated classes apparently able to inherit from sealed classes? ...

How to avoid false positives using a mockist approach in unit tests?

Since the datastructure of my application domain is becoming pretty complex as of late, I started reading up on mock objects. Soon a simple question came to my mind, but the answer has proven to be quite the headache so far. So here goes: We have a class 'Foo' with 'bar' as one of its methods: class Foo { public String bar(int i){ ...

Mock Object Libraries in Java

Would anyone suggest a Mock library and provide the reasoning behind the pick? I am looking to introduce one to the existing code base. Thanks. ...

MOQ 4.0: The type initializer for 'Moq.Mock`1' threw an exception.

I'm getting the exception The type initializer for 'Moq.Mock`1' threw an exception. using Moq 4.0 I've checked around on a couple of forums and they allude to using the Moq-NoCastle version. I've tried both this and version in the Moq folder. Both with the same result. I've got a solution with 2 projects, one for my interface...

Groovy MockFor - how to handle calls to "with" methods on mocks

I faced with interesting problem while using mocking support in Groovy to test collaboration with dependency. We have two classes (example): class Dependency { void method() { throw new OperationNotSupportedException() } } class Dependent { Dependency dependency void useDependency() { dependency.with ...

Problem mocking hibernate's SessionFactory using Mockito

Any idea why the following mocking code does not work? org.hibernate.SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); org.hibernate.Session session = Mockito.mock(Session.class); Mockito.when(sessionFactory.getCurrentSession()).thenReturn(session); The thenReturn statement does not compile. "The method thenReturn(Se...

Newbie question about mocking and moq framework

I've been using Moq framework in c# for mocking in unit tests however there is one thing I dont complete understand yet. I have this line of code var feedParserMock = new Mock<ApplicationServices.IFeedParser>(); feedParserMock.Setup(y => y.ParseFeed(csv)).Returns(items).Verifiable(); The second line does it mean it will only return th...

Testing custom ModelBinder against HTTP context in ASP.NET MVC (1.0)

Hi, I'm trying to unit test a custom model binder - specifically, I want to see how it responds to various (possibly conflicting) values being submitted in the Request.Form and Request.QueryString collections - i.e. if I submit one value in the form and another in the querystring (yeah, yeah, I know, this is evil, but I want test covera...

mock https request in java

Let's say I'm writing an application and I need to be able to do something like this: String url = "https://someurl/"; GetMethod method = new GetMethod(URLEncoder.encode(url)); String content = method.getResponseBodyAsString(); Is there a way to provide a mock server that would let me handle the https request? What I'm looking for is ...

Problem with TempData and Faked HttpContext using ASP.NET MVC

Hi Everyone, I am working with a faked HttpContext (code provided in the end) and probably I am missing something because I can't access TempData collection (forth line of SetFakeControllerContext method). Every time I try I get this error message: 'controller.TempData' threw an exception of type 'System.AccessViolationException' The ...

Mocking/Stubbing an Application Controller method with Mocha (Using Shoulda, Rails 3)

While writing functional tests for a controller, I came across a scenario where I have a before_filter requesting some information from the database that one of my tests requires. I'm using Factory_girl to generate test data but I want to avoid hitting the database when its not explicitly needed. I'd also like to avoid testing my before_...