Is there a jest way to mock Kotlin libraries? - unit-testing

I know that with jest, you can mock the imports or libraries that other functions use, but can it be done in Kotlin?
I have a class that I am writing unit tests for, but one of the functions in that class interacts with AWS by calling ProfileCredentialsProvider() and DefaultAWSCredentialsProviderChain(). I want to mock the calls to these com.amazonaws.auth libraries, but my ineffective googling has not worked out, and so the test actually tries to connect to AWS, instead of mocking the calls to the amazonaws libraries.
Does anyone have any suggestions? Or is this not a thing?

Related

Is there any mocking framework that allows to mock std::ifstream and other I/O libraries?

I work on a really old C++ project which has a huge DB I need to mock.
I am trying to mock ifstream library in order to test the control on files, the problem is that I can't change my legacy code so i am having a hard time finding the abilty to mock without changing any piece of the code
Thanks in advance
Mocking uses polymorphism (implements a given interface), so there are two choices:
use dynamic polymorphism when the library offers the option. For ifstream, this is not the case AFAIK.
use static polymorphism (templates), meaning that the legacy library offers the options to use something else than if stream.
I don't think your legacy application offers any of these options, so you will have to test it without mocks for now.

How to set data-driven tests for C++ tests using MSTest?

I am trying to use microsoft unit tests for native code, however after writing basic tests i have run into a problem: i could not find how to make test methods with parameters.
When searching for the topics, i found some ways to add such methods, namely to write data-driven tests (for example, http://msdn.microsoft.com/en-us/library/ms182527.aspx). However i am at loss as to how to use it together with native C++ tests.
As far as i could determine, no TEST_METHOD macro exists for functions with parameters, no macro for 'DataSource' attribute exists etcetera.
Is there an example of setting up data-driven test of native c++ code using MsTest?
Data Driven tests are not supported in Native C++ MSTest Framework.

C++ Unit Testing and stubbing a 3rd party C library

I need to unit test some C++ objects that I've written that use a 3rd party C library. For reasons beyond the scope of this question, I can't call the 3rd party C library directly, and need to stub it out for the test suite.
For other parts of our unit test suite we use googlemock, but I don't think it can be used for C libraries. I can stub out the library manually, but prefer not to (partly due to laziness (its rather large), but mainly because its just a matter of principles).
So here's my question: is there a tool that generates stubbed code based on a C library header file? Once I have the stubbed-out code, I'll do some minor mods to it, then I'll link against it for the unit testing.
stubgen can generate stub members from header files, unless you have special requirements it should be able to do what you're looking for.
You cant wrap those calls in a class like described in http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Free_Functions
Then You can inject (in dynamic or static way) this class and set expectation on it.

Is it OK to use MS Fakes shims with NSubstitute mocks?

We are using NSubstitute to mock external objects for our unit tests. However many legacy classes are not called via interfaces and can't be easily replaced with mocks.
I've considered to use Microsoft Fakes, but according to the answer for the question "Mock framework vs MS Fakes frameworks"
" if you're already using a more full-featured mocking framework, you
might feel like there are some important pieces missing from Fakes
stubs."
Would it be possible to use MS Fakes shims with NSubstitute mocks in the same tests?
Should we expect any compatibility problems?
Yes. I've been using Shims together with NSubstitute for several months. It work fine both locally and on build server. The only trouble is Resharper test runner does not work with Shims, so we have to use VisualStudio's test runner instead.
Yes, you should be able to use Shims with mocking frameworks.

Unit Testing in Qt with Highly Dependent Functions

I'm new to unit testing. I want to code unit tests in Qt, but my functions(login, request etc...) heavily depend on other resources such as a server.
How can I supply a block box in this situation?
Do you know any open source project which I can examine unit test classes for similar situations?
There's a linker trick that you can use. You know which methods and classes your code uses. Get a header files and declarations of those classes and make a small implementation of each that returns values you would like. Then compile those and link in right order. This will then use your own implementation of those methods and you don't need to have the right implementations that require server access.
See for example:
http://en.wikipedia.org/wiki/Mock_object
http://martinfowler.com/articles/mocksArentStubs.html
for more details.
PS. Advantage of this linker behaviour is that you don't need to declare your classes as interface first like for example, google mocking framework requires.
You need to use mocking. Google have a C++ mocking framework. You will also need to re-design your code to use interfaces in place of socket code, etc. which are replaced with mock objects when you run your tests.
Take a look at QTestLib. This is the unit testing framework that comes with Qt. Qt allows you to simulate events like mouse click and keyboard events (signals) as well as to snoop on events (signals) generated by your application.