OCmock and MKReverseGeocoder - ocmock

I would like to test a method that uses reverse Geocoding. What i would like to do is :
set the geocoder as a property of my controller
create the geocoder in the init method
call the geocoder in the method i want to test
replace the geocoder with a mock in my test
The problem is that the MKReverseGeocoder coordinate property is read only, i can only set it in the constructor method :
[[MKReverseGeocoder alloc] initWithCoordinate:coord]
And of course the coordinates are only available in the method i want to test..
Does anyone knows how i could mock the MKReverseGeocoder class ?
Thanks in advance,
Vincent.

Check out Matt Gallagher's great article on unit testing Cocoa applications. He provides a category extension to NSObject that allows you to replace instances at test time. I've used it to do something similar. I think your test would look something like this:
#import "NSObject+SupersequentImplementation.h"
id mockGeocoder = nil;
#implementation MKReverseGeocoder (UnitTests)
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
if (mockGeocoder) {
// make sure the mock returns the coordinate passed in
[[[mockGeocoder stub] andReturn:coordinate] coordinate];
return mockGeocoder;
}
return invokeSupersequent(coordinate);
}
#end
...
-(void) testSomething {
mockGeocoder = [OCMockObject mockForClass:[MKReverseGeocoder class]];
[[mockGeocoder expect] start];
// code under test
[myObject geocodeSomething];
[mockGeocoder verify];
// clean up
mockGeocoder = nil;
}

Related

Code coverage doesn't mark a line that uses <T> as passed

I don't know why i'm having this behavior with my code coverage, maybe someone knows the reasson.
As you may know, code coverage is blue when reached, red when not reached, yellow when partially reached a line of code.
I coded a little mapper that receives a IDataReader and turns into object thanks to reflection.
internal IEnumerable<T> Convert<T>(System.Data.IDataReader dataReader) where T : new()
{
var columns = this.GetColumns(dataReader); // get a list of column name... not important.
var result = new List<T>();
while (dataReader.Read())
{
var nuevoObjeto = new T(); // <-- this line is yellow in code coverage.
foreach (var item in columns)
{
var pi = nuevoObjeto.GetType().GetProperty(item);
pi.SetValue(nuevoObjeto, dataReader[columns.IndexOf(item)]);
}
result.Add(nuevoObjeto);
}
return result;
}
As you can see, the yellow line is not a conditional like IF or WHILE... is just a simple "new T"
And if you debug this code, debug reaches very well that line, in fact test is green with expected results.
Test performs this steps.
Fake IDataReader (with nSubstitute)
Only 1 result on the datareader.
T is tested with only one TestClass... like USERTEST.
hope someone knows why this happens...
thanks!
The IL that gets generated for that line is equivalent to:
T nuevoObjeto = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
This allows structs to be used for the generic type as they satisfy the new constraint.
So, if you want 100% code coverage, test your generic method with a struct or change the constraint to require that the generic type be a class using where T : class
Try to test it also with struct or add class constraint to the generic method - http://www.codeproject.com/Tips/175592/Code-Coverage-And-Generics

How to override the NSPersistentStoreCoordinator in UIManagedDocument

I have an application that uses Core Data through UIManagedObjectDocument. I am trying to add encryption to the underlying SQLite database by using Encrypted Core Data (https://github.com/project-imas/encrypted-core-data). From the description for ECD, I need to create a new type of NSPersistentSroreCoordinator. However, I cannot seem to do this with a UIManagedObjectDocument since it creates its own internal NSPersistenStoreCoordinator (which s marked private).
I create the database with this line:
UIManagedDocument* managedDoc = [[UIManagedDocument alloc] initWithFileURL:url];
I tried subclassing UIManagedDocument and creating it this way with no luck:
UIManagedDocument* managedDoc = [[EncryptedManagedDocument alloc] initWithFileURL:url];
And my implementation of the class:
#interface EncryptedManagedDocument()
#property (nonatomic,retain,readonly) NSPersistentStoreCoordinator *encryptedStoreCoordinator;
#end
#implementation EncryptedManagedDocument
#synthesize encryptedStoreCoordinator = _encryptedStoreCoordinator;
-(NSPersistentStoreCoordinator*)encryptedStoreCoordinator
{
if (_encryptedStoreCoordinator)
return _encryptedStoreCoordinator;
_encryptedStoreCoordinator = [EncryptedStore makeStore:[self managedObjectModel]:#"SOME_PASSCODE"];
return _encryptedStoreCoordinator;
}
-(NSPersistentStoreCoordinator*)persistentStoreCoordinator
{
return self.encryptedStoreCoordinator;
}
#end
Does anyone know the right way to do this?
Thanks!
I'm going to say this is not possible. UIManagedDocument wraps things up nicely and is a great time saver for the common case, but to enable my scenario I created an EncryptedManagedDocument class that is similar to UIManagedDocument but gave me control to create my own persistent store coordinator.
Thanks, everyone.

Is it possible to 'expect' a method from a partial mock

It is not the best thing to do, but I would like to verify that a private method of an object is called, so I create a partial mock and add an expectation on the private method.
Synchronizer * sync = [[Synchronizer alloc] initWithCleanup:YES];
sync = [OCMockObject partialMockForObject:sync];
[[(id)sync expect] cleanupPreviousContents];
When I run the test, cleanupPreviousContents is not called but the test is still successful. Where is the bug ?
Regards,
Quentin
Yes, this is a perfectly valid thing to do. But you need to create a new reference for your partial mock:
Synchronizer * sync = [[Synchronizer alloc] initWithCleanup:YES];
id mockSync = [OCMockObject partialMockForObject:sync];
[[mockSync expect] cleanupPreviousContents];
... do something
[mockSync verify];
Is cleanupPreviousContents called within your initWithCleanup method? If so, you'll have to structure it a bit differently:
Synchronizer *sync = [Synchronizer alloc];
id mockSync = [OCMockObject partialMockForObject:sync];
[[mockSync expect] cleanupPreviousContents];
[sync initWithCleanup:YES];
[mockSync verify];

Moq Example using out and ref needed

I am trying to build a test against some legacy method that implement out parameters.
Could you give me an example how to do this?
Just assign the out or ref parameter from the test.
Given this interface:
public interface ILegacy
{
bool Foo(out string bar);
}
You can write a test like this:
[TestMethod]
public void Test13()
{
string bar = "ploeh";
var legacyStub = new Mock<ILegacy>();
legacyStub.Setup(l => l.Foo(out bar))
.Returns(true);
Assert.IsTrue(legacyStub.Object.Foo(out bar));
Assert.AreEqual("ploeh", bar);
}
Anything wrong with the second example at the top of https://github.com/moq/moq4/wiki/Quickstart ? You really should be giving examples of what you're trying to do if you're not going to look for things like this.
Incidentally if you want to use moq (currently) to mock the out parameter too you'll also have to do the following hoop jump. Lets say that you wanted to mock an out parameter that returned another mocked object e.g.
var mockServiceA = new Mock<IMyService>();
var mockServiceOutput = new Mock<IMyServiceOutput>();
// This will not work...
mockServiceA.Setup(svc => svc.DoSomething(out mockServiceOutput.Object));
// To have this work you have to do the following
IMyServiceOutput castOutput = mockServiceOutput.Object;
mockServiceA.Setup(svc => svc.DoSomething(out castOutput));

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor) and another like this=>InsertJCDC(fakeBox).
When I stub out the parent I want to return fakePor. But when I run the code it returns null instead. Here is the unit test.
[Test]
public void PorBLL_InsertByPorInsertCV_DoingGoodCase()
{
// Startup object mapper
_Bootstrapper.Bootstrap();
// create the mock for generic Crud
IGenericCrud mockGenericCrud = MockRepository.GenerateMock<IGenericCrud>();
PorInsertCV fakePor = new PorInsertCV();
PorBoxInsertCV fakeBox = new PorBoxInsertCV();
// build fake return
PorEO fakePorNewRow = new PorEO();
fakePorNewRow.PorId = 22;
// stub parent and child insert routines.
mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow);
mockGenericCrud.Stub(c => c.InsertJCDC<PorBoxEO, PorBoxInsertCV>(fakeBox)).Return(null);
ObjectFactory.Inject(typeof(IGenericCrud), mockGenericCrud);
IPorBLL localWithMock = ObjectFactory.GetInstance<IPorBLL>();
// build user args to csll bll with and use for insert
PorInsertCV userArgs = new PorInsertCV();
userArgs.AccessionNbr = "364-80-0007";
userArgs.NbrBoxes = 11;
userArgs.RegId = 20;
userArgs.TransmitedDt = Convert.ToDateTime("1/30/1980");
// call the bll using the stub
localWithMock.InsertByPorInsertCV(userArgs);
}
Any help is greatly appreciated
I can't really follow your code that well, but I'll give it a shot.
From my skills of deduction, this line here is the one giving you issues:
mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow);
Because you're expecting fakePorNewRow to be returned when you call localWithMock.InsertByPorInsertCV(userArgs); - yeah?
If that's your case, what your problem is, is that it will only return fakePorNewRow when it is given fakePor ... not userArgs as you have given it.
Tell me if I'm completely off track.
HTHs,
Charles
Ps. You might want to add the tag of which mocking framework you are using to the question.

Resources