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];
Related
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.
This is a super newbie question on Flex. While I am a seasoned programmer, this is my first ever Flex application; please bear with me for my Flex codes.
I have a web service written in ColdFusion. In this web service, there are two functions; one is to return all the questions in a quiz and the other one is to return all the answer selections to the questions in a quiz.
[Bindable]
private var questionArray:ArrayCollection;
private var cfquiz:RemoteObject;
private function loadQuestions():void {
currentQuestionCounter = 0;
btnPrev.enabled = false;
btnNext.enabled = false;
cfquiz = new RemoteObject("ColdFusion");
cfquiz.source = "CFCertExam.cfquiz";
cfquiz.addEventListener(ResultEvent.RESULT, resultHandler);
}
private function resultHandler(event:ResultEvent):void {
questionArray = event.result as ArrayCollection;
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
btnNext.enabled = true;
}
I have the codes above. loadQuestions is called at creationComplete to retrieve the questions. Things are working fine. What I want to do is to call another function within the same web service, returnAnswers, to return the answer options for a question. Since I have cfquiz associated to the web service already, I was using cfquiz to call returnAnswers. However, there is an event listener associated to cfquiz already, resultHandler is being called when returnAnswers comes back with the results.
My questions are, first, is it possible to check which function returns the results within resultHandler? If so, how? And second, what is the best way to handle calls to multiple functions within the same web service?
Thanks in advance,
Monte
Yes you can. You need to specify a handler function for each method which in turn calls a different webservice.
The better way to do this is using AsyncToken and AsyncResponder instead of addEventListener, as following code.
tokenA = cfquiz.methodA();
tokenA.addResponder(new AsyncResponder(onResultForMethodA, onFaultMethodA));
tokenB = cfquiz.methodA();
tokenB.addResponder(new AsyncResponder(onResultForMethodB, onFaultMethodB));
tokenC = cfquiz.methodA();
tokenC.addResponder(new AsyncResponder(onResultForMethodC, onFaultMethodC));
or
tokenA = cfquiz.methodA();
var responderA:IResponder = new AsyncResponder(onResult, onFault, "methodA");
tokenB = cfquiz.methodB();
var responderB:IResponder = new AsyncResponder(onResult, onFault, "methodB");
tokenA.addResponder(responderA);
tokenB.addResponder(responderB);
private function onResult(evt:ResultEvent, token:Object):void {
if(token == "methodA" ) {
//logic for methodA
}
if(token == "methodB" ) {
//logic for methodB
}
}
I'm a little confused as I can't see where you are calling the actual web service function, for example from these examples, I am expecting to see:
cfquiz = new RemoteObject("ColdFusion");
cfquiz.source = "CFCertExam.cfquiz";
cfquiz.addEventListener(ResultEvent.RESULT, resultHandler);
cfquiz.myCFCFunctionCall(); /* where is this? */
Anyway, AFAIK you can create a new instance of the remote object and set that up to have it's own event listener.
Hope that helps.
What I'm doing NOW:
Often multiple instances of the view component would be used in multiple places in an application. Each time I do this, I register the same mediator with a different name.
When a notification is dispatched, I attach the name of the mediator to the body of the notification, like so:
var obj:Object = new Object();
obj.mediatorName = this.getMediatorName();
obj.someParameter = someParameter;
sendNotification ("someNotification", obj);
Then in the Command class, I parse the notification body and store the mediatorName in the proxy.
var mediatorName:String = notification.getBody().mediatorName;
var params:String = notification.getBody().someParameter;
getProxy().someMethod(params, mediatorName);
On the return notification, the mediatorName is returned with it.
var obj:Object = new Object();
obj.mediatorName = mediatorName;
obj.someReturnedValue= someReturnedValue;
sendNotification ("someReturnedNotification", obj);
In the multiple mediators that might be watching for "someReturnedNotification," in the handleNotification(), it does an if statement, to see
if obj.mediatorName == this.getMediatorName
returns true. If so, process the info, if not, don't.
My Question is:
Is this the right way of using Multiton PureMVC? My gut feeling is not. I am sure there's a better way of architecting the application so that I don't have to test for the mediator's name to see if the component should be updated with the returned info.
Would someone please help and give me some direction as to what is a better way?
Thanks.
I checked with Cliff (the puremvc.org guy) and he said it's fine.
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;
}
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.