chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
The following test fails to run (if a line marked "Incorrectly Fails" is commented out the test will fails on the next line with such a mark):

// generic_class_mocking.h

#pragma once

using namespace System;
using namespace TypeMock;
using namespace MbUnit::Framework;

namespace generic_class_mocking {

public ref class A { };

generic<typename T>
public ref class Base
{
static T _sbt;
static property T sbt
{
T get() { return _sbt; }
void set(T t_) { _sbt = t_; }
}
T _bt;
property T bt
{
T get() { return _bt; }
void set(T t_) { _bt = t_; }
}
static A ^_sba;
static property A ^sba
{
A ^get() { return _sba; }
void set(A ^a_) { _sba = a_; }
}
A ^_ba;
property A ^ba
{
A ^get() { return _ba; }
void set(A ^a_) { _ba = a_; }
}
};

public ref class Derived : Base<A^>
{
static A ^_sda;
static property A ^sda
{
A ^get() { return _sda; }
void set(A ^a_) { _sda = a_; }
}
};

public ref class Derived2 : Base<Derived2^>
{
};

[TestFixture]
public ref class TestSuite
{
[Test]
void Test()
{
A ^a = gcnew A;
Mock ^mock = MockManager::MockAll(Derived::typeid);
mock->ExpectGetAlways("ba", a); //OK
mock->ExpectGetAlways("bt", a); //OK
mock->ExpectGetAlways("sda", a); //OK
mock->ExpectGetAlways("sba", a); //Incorrectly Fails: *** No method get_sba in type generic_class_mocking.Derived returns generic_class_mocking.A
mock->ExpectGetAlways("sbt", a); //Incorrectly Fails: *** No method get_sbt in type generic_class_mocking.Derived returns generic_class_mocking.A
Derived2 ^derived2 = gcnew Derived2;
Mock ^mock2 = MockManager::MockAll(Derived2::typeid);
mock2->ExpectGetAlways("sbt", derived2); //Incorrectly Fails: *** No method get_sbt in type generic_class_mocking.Derived2 returns generic_class_mocking.Derived2
}
};
}
asked by jho1965dk (1.7k points)

1 Answer

0 votes
Hi
The problem is that you should mock the base class
instead the derived class.
Static methods are owned by the class that defines them and not to the derived implementation.

try:
Mock ^mock = MockManager::MockAll(Base<A^>::typeid);


By the way if you want to mock public properties you can do it
in natural mocks in 'nicer' (natural :wink:) way

[Test]
void Test2()
{         
   {
      RecordExpectations ^r = RecorderManager::StartRecording();
      A ^mockA = Derived::PublicProperty;
      r->Return(gcnew A())->RepeatAlways();
        // ...
   }
}


Hope it helps.
answered by ohad (35.5k points)
...