mocking

Using Moq .GetMock to register ~1IRepository with Linq Expression?

Is it possible to Mock a Linq Expression via Moq using a Generic class such as ~1Repository. ~IRepository being something that is injected via an IoC such as StructureMap or Windsor? CODE TO TEST: var person = _repository.Find<Person>() .Where(p => p.Id == person.Id).SingleOrDefault(); TEST: var rep...

How to test a ruby application which uses mechanize

Hi, I wrote a small program that uses Mechanize to traverse a site. I want to write tests for it, but don't want it to actually go log onto the site every time I run the tests. I would like to mock the internet, so that when it goes to some site, it simply returns the stored results. Here is a small example, pretend my code's purpose ...

How to mock a method with functional arguments in Scala?

I'm trying to mock a method call that takes a call-by-name argument: import org.scalatest.WordSpec import org.scalatest.mock.MockitoSugar import org.mockito.Mockito._ import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner trait Collaborator { def doSomething(t: => Thing) } trait Thing @RunWith(classOf[JUnitRunner])...

Generic Constraints vs. Inheritance

I'm trying to write some code to help unit test WCF services. These services are accessed through a facade class that creates the proxy instance, then calls the proxy method and returns the result; for each proxy method. I'd like to be able to replace the current creation code with something that either creates the real service or a fake...

Mocking ActiveRecord relationship beheavior in RSpec tests

I've run into this problem with testing. Let's assume I have two models, User and Post, where user has_many :posts. I'm trying to spec out a code block that includes something like this: user = User.find(123) post = user.posts.find(456) I know how to mock out the User.find and user.posts parts. The user.posts mock returns an array of...

Should I use an In-Memory Database instead of mocking out my Repositories?

I like the idea of using an In-Memory Database such as SQLite when testing instead of creating Mocks for my Repositories. This way I can also test the code of my Repositories without any performance issues. What are the pros and cons of this strategy? ...

Creating Mock object of Interface with type-hint in method fails on PHPUnit

I created the following interface: <?php interface Action { public function execute(\requests\Request $request, array $params); } Then I try to make a Mock object of this interface with PHPUnit 3.4, but I get the following error: Fatal error: Declaration of Mock_Action_b389c0b1::execute() must be compatible with that of Action::e...

How to moq a NetworkStream in a unit test?

Hi everyone! I'm using Moq & NUnit as a unit test framework. I've written a method that is given a NetworkStream object as a parameter: public static void ReadDataIntoBuffer(NetworkStream networkStream, Queue dataBuffer) { if ((networkStream != null) && (dataBuffer != null)) { while (networkStream.DataAvailable) { ...

How to make a unit test mock of an object with non-virtual functions

I have a C# class that gets generated using the wsdl.exe tool that looks something like this: public partial class SoapApi : System.Web.Services.Protocols.SoapHttpClientProtocol { public SOAPTypeEnum AskServerQuestion() { object[] results = return this.Invoke("AskServerQuestion"); return (SOAPTypeEnum) results[0]...

How to mock ASP.NET 3.5 to unit test my web controls?

I want to mock ASP.NET 3.5 behavior in order to unit test my WebControls: I want to test how they perform with mock data with existing system of events. Basically I want to test generated result HTML based on input mock data. How to do it? I looked into NMock, but it doesn't suit my needs for 2 reasons: It just runs ASP.NET server in...

Troubles with simple mocking using RhinoMocks .NET

I am trying to experiment with RhinoMocks, where I have to say I am a newbie and probably I don't get some obvious thing here. What I'm doing is something like : [TestMethod] public void SaveResponsibleUserFromChangeset() { var action = mocks.StrictMock<GenomeAction>(); var changeset = new ActionChangeset(); ...

How can I mock a collection using Moq

I'm brand new to unit testing and mocking and still wet behind the ears. I'm using the Moq framework and I need to mock a collection such that it yields a single member with a value I supply. The collection class in question is a System.Configuration.SettingsPropertyCollection, which contains SettingsProperty objects. In turn, the Sett...

Should I test UDP server code, and if so - why and how?

I don't have much experience doing unit testing. From what I learned, code should be decoupled, and I should not strive to test private code, just public methods, setters, etc etc. Now, I have grasped some basic testing concepts, but I have troubles applying more advanced stuff to this case... Dependency Injection, Inversion of Control,...

Mock Y of (from X import Y) in doctest (python)

I'm trying to create a doctest with mock of function that resides in a separate module and that is imported as bellow from foomodule import foo def bar(): """ >>> from minimock import mock >>> mock('foo', nsdicts=(bar.func_globals,), returns=5) >>> bar() Called foo() 10 """ return foo() * 2 import doct...

How can I bypass the execution of a method in a RhinoMocks mock ?

I use RhinoMocks for a very simple test (I have to say I'm a beginner here). I tried to mock my object like this var mock = MockRepository.GenerateMock<MyClass>(); create a helper stub : var stubLinkedObject = MockRepository.GenerateStub<MyClass>(); then execute some logic which should call the method AddLink of the class MyClass ...

How do I allow an implementation to call a Mock (Moq) method with its own arguments?

When testing a class that uses a Moq dependency, I'd like the class to supply the arguments to a Moq method, rather than creating the arguments myself in the test case. I have an interface, that has one method, which takes one argument. public interface IMessageForwardProxy { IMessage Send(IMessage request); } I have a class that...

Flex Unit - testing a library wrapping remote objects

I'm wrapping RemoteObject inside a class for easier managing of retries, timeouts, failures and such non standard scenarios. So when wrapping a RemoteObject inside another class, how would I go about unit testing this? Here is an example of how to use the class: // set up the object as you would a RemoteObject, but without events: var...

Is it possible (with Moq) to stub method calls with Lambda parameters?

If I do this: var repository = new Mock<IRepository<Banner>>(); repository.Setup(x => x.Where(banner => banner.Is.AvailableForFrontend())).Returns(list); "Where" is a method on my repository that takes a Func<T, ISpecification<T>. AvailableForFrontend returns an implementation of ISpecification, and list is an IEnumberable of the gene...

Groovy mock Java class with parameters

I'm trying to use groovy's MockFor and proxyDelegateInstance to mock a java class with constructor parameters, but I can't seem to get it right. My Java class looks like: class MyJavaClass { private MyObject myObj public MyJavaClass(MyObject myObj) { this.myObj = myObj; } } class MyGroovyTest { @Test void testMyJ...

Mock web service

Hi, We have two components: enterprise application X, and Web service Y We want to make our (automated) testing tool that will test application X (that interact with Y) only, and we have not the web service Y available. Notes: The testing tool will be a desktop application. We Don't want to use another external tools-e.g. SoapUI- fo...