Suppose I have some code
type SomeObject struct {
user *models.User
}
func (obj *SomeObject) DoSmthLogic(val1 int, val2 int) error {
// do step 1
// do step 2
// do step 3
}
I decided to cover this code with unittests.
Then for convenience i decided to mode steps to seprated method and cover each method.
type SomeObject struct {
user *models.User
}
func (obj *SomeObject) DoSmthLogic(val1 int, val2 int) error {
result = obj.step1()
obj.step2(result)
obj.step3()
}
func (obj (SomeObject) step1() int {
// do smth
}
// the same for other steps
Not it's easy to cover these 3 methods.
But how can I cover DoSmthLogic method?
For example in Python I can easly mock steps methods and check in unittests if these methods were called:
obj.step1 = Mock()
obj.step2 = Mock()
obj.step2 = Mock()
obj.do_smth_logic()
obj.step1.assert_called_once()
obj.step2.assert_called_once()
obj.step3.assert_called_once()
What can you suggest for this?
Related
I am new to google test environment. I have a sample code written in C and want to perform unit test with Google test framework.
Below is the sample code
// My module function (test.c)
void Update_user_value(void)
{
int user_value;
user_value = get_val_module(); /* return a integer value*/
if(user_value == 0x1)
update_user_flag(true);
else
update_user_flag(false);
}
// This function is in the other module(stub.c) , so we can use Mock function
void update_user_flag(bool val)
{
struct *temp;
if(val == true)
{
temp->userflag = 1;
}
else
{
temp->userflag = 0;
}
}
I wan to write a test case for the Update_user_value function (only for test.c). Through this function, i am sending some flag value to other module (update_user_flag) to set the flag.
I have written a simple google test like this
TEST_F(sampletest, setuser_flag_true_testcase)
{
//Get value from module
ON_CALL(*ptr1, get_val_module()).WillByDefault(Return(0x1)); //Mock function
EXPECT_CALL(*ptr2, get_val_module(_)).Times(1); // Mock function
Update_user_value();
}
TEST_F(sampletest, setuser_flag_false_testcase)
{
//Get value from module
ON_CALL(*ptr1, get_val_module()).WillByDefault(Return(0x0)); //Mock function
EXPECT_CALL(*ptr2, get_val_module(_)).Times(1); // Mock function
Update_user_value();
}
My question: Is this test case is enough to validate the Update_user_value function ?
Also i want to know, EXPECT_CALL() is good to use for setting a value to other module ?
If my understanding is wrong, please suggest me a better test case ?
ON_CALL, EXPECT_CALL are macros designed to be used on mock objects. Usually the use case is as follows:
You create an interface to derive from (it will be mocked in your test).
You pass the mock object (to method or via dependency injection).
You make expectations on this object.
See example:
class Foo {
public:
virtual ~Foo() = default;
virtual int bar() = 0;
};
class FooMock : public Foo {
public:
MOCK_METHOD0(bar, int());
};
bool check_bar_over_42(Foo& foo) {
if (foo.bar() > 42) {
return true;
}
return false;
}
TEST(check_bar_over_42_test, bar_below_42) {
FooMock fooMock{};
EXPECT_CALL(fooMock, bar()).WillOnce(testing::Return(41));
ASSERT_FALSE(check_bar_over_42(fooMock));
}
TEST(check_bar_over_42_test, bar_above_42) {
FooMock fooMock{};
EXPECT_CALL(fooMock, bar()).WillOnce(testing::Return(43));
ASSERT_TRUE(check_bar_over_42(fooMock));
}
AFAIK there is no way of using EXPECT_CALLs on C-like functions. One approach for your problem would be link-time mocking: given a method get_val_module is defined in a separate library, you can create test-only library with get_val_module that will allow you to return the expected values. In tests you would link against test lib, in production - against the real lib.
I'm having a hard time trying to get a private method in Kotlin using reflection in order to pass it as a parameter to a higher order function, here is what I got and what I need to do:
The function that gets the private method, probably what I should change or fix:
inline fun <reified T> T.getPrivateFunc(name: String): KFunction<*> {
return T::class.declaredMemberFunctions.first {
it.name == name
}.apply {
isAccessible = true
}
}
This is the high order function I have:
class MyService {
fun myHigherOrderFunction(action: () -> Unit) { /*...*/ }
}
These are the class and the private method I need to get somehow:
class SystemUnderTest {
fun privateFunc() { /*...*/ }
}
Finally a unit test where I I'm trying to make sure the proper method is passed to the high order function, I omitted details for simplification:
// ...
val serviceMock = MyService()
val sut = SystemUnderTest()
// Here is what I'm trying to accomplish
val privateMethod = sut.getPrivateMethod("privateFunc")
service.myHighOrderFunction(privateMethod)
// In the above line I get a compilation error: required () - Unit, found KFunction<*>
service.myHigherOrderFunction(privateMethod as () -> Unit)
// In the above line I get the following runtime error:
// ClassCastException: kotlin.reflect.jvm.internal.KFunctionImpl cannot be cast to kotlin.jvm.functions.Function1
I know the test can be done having the privateFunc as public and maybe annotating it with #VisibleForTesting, but what I want is to avoid compromising the design as long as I can.
Any ideas? Thanks in advance!
I don't think KFunction and KCallable have any notion of a bound receiver, so they are not invokable (have no operator fun invoke), and therefore don't qualify as functions. So I think you have to wrap the KFunction object in a function to be able to pass it to your higher order function. To call a KFunction, you pass the instance of the receiver class as the first argument.
val serviceMock = MyService()
val sut = SystemUnderTest()
val privateMethod = sut.getPrivateMethod("privateFunc")
service.myHighOrderFunction { privateMethod.call(sut) }
Edit: To internalize the creation of the wrapped function, you could do this:
inline fun <reified T> T.getZeroArgPrivateMethod(name: String): () -> Unit = {
T::class.declaredMemberFunctions.first {
it.name == name
}.apply {
isAccessible = true
}.call(this)
}
//...
val serviceMock = MyService()
val sut = SystemUnderTest()
val privateMethod = sut.getZeroArgPrivateMethod("privateFunc")
service.myHighOrderFunction(privateMethod)
I would test presenter like this:
class MostPopularPresenter #Inject constructor(val mostPopularUseCase: MostPopularUseCase)
: Presenter<MostPopularView>() {
fun requestMostPopular(page: Int, update: Boolean) {
if (page <= 6)
mostPopularUseCase.execute(MostPopularObserver(), MostPopularUseCase.Params.createQuery(page, 15, update))
}
inner class MostPopularObserver : DisposableSingleObserver<MostPopularModel>() {
override fun onSuccess(t: MostPopularModel) {
this#MostPopularPresenter.view?.populateRecyclerList(t)
}
override fun onError(e: Throwable) {
this#MostPopularPresenter.view?.showError()
}
}
}
I have problem how to mock observer and force it to throw error or return value on success. I'm using mockito/junit. Can someone point me how to achieve it? Maybe my code is untestable?
An observer is an object that shouldn't be really tested. It has been already tested when it has been developed by a third developer, although there are some people that say, with a part of reason, that you should also test a third party library in order to ensure that it doesn't break your code between versions.
So, if you don't test the observer... how do you test your code? Simply, what you really need to test is the presenter itself. The code running inside the observer is part of the presenter. So instead of mocking the observer mock the useCase:
test useCaseFails() {
val usecase = // mock use case
when(usecase.execute(...))
.thenAnswer(/* receive the observer as first parameter
and make it emit an error */)
val presenter = ...
presenter.requestMostPopular(...)
// assert that presenter.view?.showError has been called
}
Another way of doing this (at least this is the way I usually code) is to make the useCase return an observable and subscribe it in the presenter:
class MostPopularPresenter #Inject constructor(val mostPopularUseCase: MostPopularUseCase)
: Presenter<MostPopularView>() {
private var lateinit observer : Disposable
fun requestMostPopular(page: Int, update: Boolean) {
if (page <= 6)
disposable = mostPopularUseCase.execute(MostPopularUseCase.Params.createQuery(page, 15, update))
.subscribe(t -> view?.populateRecyclerList(t),
e -> view?.showError())
}
}
This way you can easily mock your useCase so it returns a Subject you can control:
test useCaseFails() {
val usecase = // mock use case
val subject = PublishSubject()
when(usecase.execute(...))
.thenReturn(subject)
val presenter = ...
presenter.requestMostPopular(...)
subject.emitError(...) // <- pseudocode
// assert that presenter.view?.showError has been called
}
Usually there are not many cases where it is absolutely not possible to test. As far as I see it, you have a few options:
Put the observer into the constructor with a default value (but this might have some downsides with your dependency injection)
Put the observer into the function with a default value. This would work, but you have to choose if your API should contain this
Use the observer as property. In the test you can override this one.
All this variants would work and are listed here:
// observer in constructor
class MostPopularPresenter #Inject constructor(val mostPopularUseCase: MostPopularUseCase, val observer: DisposableSingleObserver<MostPopularModel> = MostPopularObserver())
: Presenter<MostPopularView>() {
// observer as property
internal var observer: DisposableSingleObserver<MostPopularModel> = MostPopularObserver()
// observer in function
fun requestMostPopular(page: Int, update: Boolean, observer: DisposableSingleObserver<MostPopularModel> = MostPopularObserver()) {
if (page <= 6)
mostPopularUseCase.execute(observer, MostPopularUseCase.Params.createQuery(page, 15, update))
}
}
internal class MostPopularObserver : DisposableSingleObserver<MostPopularModel>() { ... }
It would be even nicer, if you us a DisposableSingleObserverFactory and create the observer when it's needed.
class MostPopularPresenter #Inject constructor(val mostPopularUseCase: MostPopularUseCase, val observerFactory: DisposableSingleObserverFactory<MostPopularModel> = MostPopularObserverFactorty())
: Presenter<MostPopularView>() {
internal var observerFactory: DisposableSingleObserverFactory<MostPopularModel> = MostPopularObserverFactory()
fun requestMostPopular(page: Int, update: Boolean, observerFactory: DisposableSingleObserverFactory<MostPopularModel> = MostPopularObserver()) {
if (page <= 6)
mostPopularUseCase.execute(observerFactory.create(), MostPopularUseCase.Params.createQuery(page, 15, update))
}
}
internal class MostPopularObserver : DisposableSingleObserver<MostPopularModel>() {
My question is simple, how to mock a function (not a method) in Swift.
i.e., a standalone function not inside of a class.
Thank you.
EDIT:
Let's say I have the following function:
func distance(c1: CLLocation, c2: CLLocation) {
...
}
And I want to test my class:
class MyClass {
func selectedLocation(location: CLLocation) {
let text = "\(distance(self.currentLocation, location)) meters"
self.view.showText(text)
}
}
How do I mock the distance function?
To mock the distance function, you would need to do something like this
func distance(c1: CLLocation, c2: CLLocation) -> CLLocationDistance {
// ...
}
class MyClass {
var calculateDistance = distance
func selectedLocation(location: CLLocation) {
let text = "\(calculateDistance(self.currentLocation, location)) meters"
self.view.showText(text)
}
}
And in your test code you would need to do this:
func testCalculateDistanceFromLocation() {
let thing = MyClass()
thing.calculateDistance = { c1, c2 in /* return mock distance here */ }
// assert correct text appeared in the view
}
This way you are providing a new implementation of the distance function when in a testing environment. As far as I know you cannot completely replace the body of a top level function dynamically such that you don't need the internal class property that stores that function value.
This is kind of cumbersome though to do this for all your functions, so I say to only do this when you feel you absolutely need to substitute this extra mocked dependency. If possible, I would encourage you to test your class as a wnole unit, if it has few or no other external dependencies and treat the distance function as an implementation detail.
Do not know if I understand this correct. Swift does support global functions.
[update: This is what I do in the unit test]
public func getNumber()->Int //add public for unit testing
{
return 1
}
class MyClass: NSObject
{
func calculate()
{
let num = getNumber()
println(num)
}
}
///unit test case
import MyModule
extension NSObject
{
public fund getNumber()->Int
{
return 5 //mock implementation
}
}
func testExample() {
let myInstance = MyClass()
myInstance.calculate()
}
I'm working with Golang, and currently I'm doing some fun unit test with Testify, my file look like this
type myStruct struct {
field_1 string
}
func (self *myStruct) writeFirst() {
//doing something
//modify field_1
self.writeSecond()
}
func (self *myStruct) writeSecond() {
//doing something
}
In this case I'm testing writeFirst() but I'm trying to replace writeSecond() because it is using http stuff that I don't want to use because it access to internet.
I think that use a second struct and set myStruct as anonymous field will be the solution, but it's not working because me second struct and myStruct have a diferent context.
In this case I can't use mocks cause writeSecond is a method of the struct.
My test case looks like this:
func TestWriteFirst(t *testing.T) {
myStc := myStruct{}
assert.Equal(t,"My response", myStc.field_1)
}
All that I want is testing writeFirst without pass to writeSecond()
To illustrate the kind of refactoring mentioned by Not-a-Golfer in the comments, you could consider calling your second function only on an instance that is an interface:
type F2er interface {
Func2()
}
type S struct{ _f2 F2er }
var s = &S{}
func (s *S) f2() F2er {
if s._f2 == nil {
return s
}
return s._f2
}
func (s *S) Func1() {
fmt.Println("s.Func1")
s.f2().Func2()
}
Here: Func1 calls Func2 on s.f2(), not directly s.
If nothing has been set in s, s.f2() returns... itself: s
if s._f2 was replaced by any other struct which implements Func2, s.f2() returns that instance instead of itself.
See a complete example in this playground script.
Output:
TestFunc1
s.Func1
s.Func2
TestFunc1bis
s.Func1
testS.Func2 <=== different Func2 call