XCTestcase How to mock C++ objects - xctest

I am using XCTestCase to write unit tests for my mobile devices. I have code written in C++ I would like to test. I need a way to mock some of the C++ calls. Does anyone know of a framework like OCMock that works for C++ using the XCTestCase framework? Thanks.

Related

Instantiate XCTestCase subclass multiple times in a test run

I have an XCTestCase subclass EndpointIntegrationTests that contains integration tests that exercise the client application's endpoints against the backend.
We have a number of testing backend environments. They share the same databases, so the tests in EndpointIntegrationTests should work equally well on all of the backends.
I'd like to be able to instantiate multiple instance of EndpointIntegrationTests in a test run, initialising each (or configuring after initialisation) with the specific backend it should test. I'd then instantiate the suite once for each backend, so that all tests are run against all backends. Perhaps a bit like this…
func addTestCaseInstances() {
for environment in ["SIT01", "SIT02", "NFT"] {
let testCase = EndpointIntegrationTests(environment: environment)
addTestCaseInstance(testCase)
}
}
Is there a mechanism in XCTest to allow this? I've only ever seen XCTestCase subclasses be automatically instantiated by the test harness. The harness only instantiates each subclass once.
Thanks.

Stubbing static methods in MSpec

I'm trying to test a class that utilizes a repository class that is interfaced with through static methods. The actually repository interacts with a database. I do not want to setup a database in Test, I only to make sure that the repository methods are called. In the RSpec world I would do something like allow(NodeRepository).to receive(:create).and_return(true). Is there a similar feature in MSpec or some other .NET testing tools.
It's not possible to stub static methods in. NET without extra testing tools like TypeMock Isolator. All freely available mocking tools on .NET use dynamic proxies that cannot hook into non-virtual methods (which static methods are).

Can I include a single ObjC class when compiling Swift code?

In my understanding, it will be possible to work with Obj-C classes from Swift and write test cases much more quickly, and see the results in a playground project.
Does *.playground support including just a single class, like an .m/.h pair?
How does it work? Do I need to compile this class separately, or is it done automatically?
Unfortunately, a pure playground allows to import only Cocoa framework (for now, at least).
If you want to import other modules, you need to create a playground file inside an existing project. That way, the underlying Swift code inside the playground can access your symbols.
Reference: Does swift playground support UIKit?

How does one unit test code that interacts with the Core Bluetooth APIs?

I would like to unit test a class that acts as a CBPeripheralManagerDelegate to the CBPeripheralManager class. Typically, in order to stub out an external class dependency, I would use either a form of dependency injection by passing in via the class initializer or via a property. When dealing with singleton-based API's, I have been able to use libraries like Kiwi to stub the class level method that returns the singleton (i.e. [ClassName stub:#selector(sharedInstance) andReturn:myStubbedInstance]). The issue in the case of mocking CBPeripheralManager is that its initializer takes the delegate instance. So any code that uses my class would need to do something like this:
PeripheralManagerWrapper *wrapper = [[PeripheralManagerWrapper alloc] init];
CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:wrapper queue:nil options:nil];
wrapper.peripheralManager = peripheralManager;
Then, for unit testing my PeripheralManagerWrapper class, I could simply instantiate it and pass in a mocked CBPeripheralManager. However, I don't like requiring any calling code of my wrapper object to have to go through this setup. Is there a better pattern for dealing with this situation? I've used both Kiwi and OCMockito, but neither seem to provide this functionality short of maybe stubbing the alloc and init methods of CBPeripheralManager and then just instantiating the instance in the PeripheralManagerWrapper
's initializer.
IMHO, the Core Bluetooth APIs are perfect match for unit testing. All the delegate callbacks take the manager and the relevant parameters so if you follow the pattern that you use these arguments instead of the internal state, then you will be able to pass in anything you want. Using mock objects is the best way to do this. While unit testing, you shouldn't try to mock the behavior of the managers. You should focus on verifying the interaction of your code with the API and nothing more.
Wrappers may better suit integration testing. But actually, the integration testing of Core Bluetooth code is better done manually to my experience. The stack is not stable enough to allow for reliable testing, and the test code would have to be fortified against the stack errors too which is really hard as, obviously, those are not documented or predictable just by looking at the APIs. While on the other hand, your test code would have to simulate the erroneous behavior of the stack too. There may be cases when it is possible but the test code will be just as complex if not more than the code you are testing.

How to expose templates from a C++ to C# via C++/CLI

I want to expose a C++ library to a C# application and I decided to take the C++/CLI wrapper approach instead of P/Invoke. The problem that I am now facing is how to expose a templated class in the C++ lib to the C# application using generics.
On the C++ side I have:
template <typename T> class Someclass;
And my goal is to be able to expose the following type to the C# app
class Someclass<T> {}
So the question now is how should my C++/CLI wrapper look like. I tried the naive approach and created a templated type in C++/CLI and naturally I wasnt able to instantiate the class in C# with generic parameters. And if I expose the class as a generic class I wont be able to pass it to C++.
Disclaimer: I am a C++ newbie so please be gentle :)
I am familiar with the differences between generics and templates so no need to explain those. I have this bad feeling that what I want isn't doable, but since I am relatively new to C++ I hope I can somehow achieve it.
AFAIK: You'll have to write a C++ generic.
http://www.codeproject.com/KB/mcpp/cppcligenerics.aspx
I don't see how the C# application would even understand the template being as that is calculated at compile time, so the C# application wont see templates in the DLL.