for internal method test case method is not accessible - unit-testing

I am writing unit test case for my internal method. I have made necessary changes in AssemblyInfo.cs of my mail class project
[assembly: InternalsVisibleTo("myunitest_assemblyname")]
now i can access my internal method in unit test case method.
but when i compile the code it is giving an error as below
Error 1 'main class project name' does not contain a definition for 'Process' and no extension method 'Process' accepting a first argument of type 'main class project name' could be found (are you missing a using directive or an assembly reference?)
my main class has strong name.
it would be great if some one point where i am missing.
main class structure
namespace Renewals
{
public class StateProcessor
{
internal virtual void PutEmailInQueue(DataTable dataTable)
{
}
}
}
//test class structure
namespace Renewals.Tests.Unit
{
[TestClass]
public class StateProcessorTest
{
[TestMethod]
public void PutEmailInQueueTest()
{
DateTime processingDate = Convert.ToDateTime("27-feb-2013"); ;
StateProcessor stateProcess = new StateProcessor(processingDate);
stateProcess.PutEmailInQueue(new DataTable());
}
}
}
PutEmailInQueue - this method giving me problem.

you wrote that your class use strong name.
I think you have to modify your InternalsVisibleTo() statement with the public key.
e.g.: [assembly: InternalsVisibleTo("friend_signed_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3aedce99b7e10823920206f8e46cd5558b4ec7345bd1a5b201ffe71660625dcb8f9a08687d881c8f65a0dcf042f81475d2e88f3e3e273c8311ee40f952db306c02fbfc5d8bc6ee1e924e6ec8fe8c01932e0648a0d3e5695134af3bb7fab370d3012d083fa6b83179dd3d031053f72fc1f7da8459140b0af5afc4d2804deccb6")]
for more informations see: http://msdn.microsoft.com/en-us/library/bb385180.aspx

Related

I am doing unit testing using gtest and gmock frameworks and I need help in stubbing/mocking a external C functions used inside class functions

So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ?
My production code : prod_code.cpp
int TargetTestClass::targetFunc()
{
if(externalCFunc() == True)
{
statement1; statement2; statement3; /// and so on
}
}
My testcode.cpp generally contains tests like this
//Fixture for Target Test class
class TargetTestClassFixture : public testing::Test {
TargetTestClass* targetTestClassPtr;
void SetUp() {
targetTestClassPtr = new TargetTestClass();
}
void TearDown() {
delete targetTestClassPtr;
}
};
TEST_F (unitTest, test_001)
{
targetTestClassPtr->targetFunc(); //Need to do something here so that externalCFunc() returns true on call
}
What you can do is to create a source file like my_c_stubs.c where you rudimentary implement your C function. For example, the implementation can just return true. Then don't link original source file with the external C function but rather use your stub file. You should still use the original C header. In this way you won't be able to stub inline functions though. If it is required, some more sophisticated approach is needed.
I found 2 solutions to my problem so I am going to answer the same here.
Solution 1 : This involved changing the target source code. Basically you need to write a wrapper that calls the external C functions like below
Class TargetTestClass{
protected:
int targetFunc();
virtual int externalCFuncWrapper(); // Wrapper
};
//call the external C function from the wrapper
int TargetTestClass::externalCFunctionWrapper(){
return(externalCFunc());
}
//Definition of targetFuc in original question
//Now write a mock class for Target Test Class as usual and mock the wrapper function to return what you want to
class MockTargetTestClass : public TargetTestClass{
public: MOCK_METHOD0(externalCFunctionWrapper, int());
};
//Now use the Mock class as needed
TEST_F ( TargetUnitTest, TestingExternalCFuctionCall)
{
MockTargetTestClass mockTargetTestClassObj;
using ::testing::Return;
using ::testing::_;
Expect_Call(mockTargetTestClassObj, externalCFunctionWrapper())
.WillOnce(Return(1));
Assert_EQ(mockTargetTestClassObj.targetFunc(), 1);
}
Solution 2 : Thanks to #kreynolds, I have looked into Fake Function Framework and implemented as follows :
Class TargetTestClass{
protected:
int targetFunc();
//No Code change needed in target source code
};
//In testcode.cpp
#include <gmock-global/gmock-global.h>
MOCK_GLOBAL_FUNC0(externalCFunc, int());
TEST( Unittest, test002){
using ::testing::Return;
using ::testing::_;
EXPECT_GLOBAL_CALL(externalCFunc, externalCFunc()).WillOnce(Return(1));
TargetTestClass targetFunc; //This does not contain any wrapper
EXPECT_EQ(targetTestClassObj.targetFunc(), 1);
}
I am using the second solution as this does not require any change in my source code and easier to use.
Once again thank you everyone for giving your time.

UActorComponent derived class in plugin is not reacting for function calls

So I have a little problem with creating project plugin in Unreal Engine for (4.15). So let's break it down.
1.I've created MyClass that is derived from UActor Component and has also this line:
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
2.I've added that component to my GameMode.
3.Then I'm trying to call any function from inside the class by Get GameMode then casting to MyGameMode and getting MyClassComponent.
4.When I'm trying to call function nothing happens at all.
I was trying to debug it but it never goes in function body but prints before function and after works perfectly fine. I also must say that when functions are compiled straight into project they work 100% fine!
This is sample of how do I declare my functions:
UFUNCTION(BlueprintCallable, Category = "MyClass|Test")
void TestFunction();
void UMyClass::TestFunction()
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, "hello there");
}
If there is any more information required that I'm not aware of please let me know.
MyClass declaration
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class UMyClass : public UActorComponent
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "MyClass|Test")
void TestFunction();
};
Any classes that need to be consumed publicly need to have their symbols exported.
In UE plugins this is achieved with the YOURPLUGIN_API specifier, where YOURPLUGIN is the name of the plugin.
This in turn is defined as __declspec(dllexport) when exporting and __declspec(dllimport) when consuming the plugin.
So your class definition should look like:
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class MYPLUGIN_API UMyClass : public UActorComponent
{
...
}

Access Form controls in different namespace and class C++

I have a rather decent sized project and I am trying to split it up in multiple files. I am using MSVC++ 2010 as a Windows Form's application. I am trying to be able to access the form controls from the namespace and class from a user defined class in a separate file. The main autogenerated class is defined as so:
main.cpp:
namespace MyMainForm {
...
...
...
public ref class Form1: public System::Windows::Forms::Form
{
...
...
};
}
Than I have a class I created that I want to be able to access the GUI components that are in the Form1 class although they are "private" So I tried inherting as MyMainForm::Form1 but does not seem to work.
mynewclass.cpp:
class MyNewClass {
...
...
};
Any thoughts? Thanks for your help!
Edit:
When inherting like so:
class MyNewClass : MyMainForm::Form1 {
I get an error like this:
error C2654: 'MyMainForm' : is not a class or namespace name
error C2504: 'Form1' : base class undefined
The name of MyMainForm IS a namespace though defined in the main.cpp

C++: class "X" has no member named "Y"

I have two projects: Project A & Project B.
In Project A, I have the class: 1.
I've included the class '1' from Project A using "#include '1' & using the configuration in eclipse.
When I try to access a public method from class 1 in Project B I'm getting the error:
Class "A" has no member named "Test".
What am I doing wrong???
EDIT: The class name & method is for skeleton purposes.
Project "MGeneral" has a class called "MGeneralCommands" -
class MGeneralCommands
{
public:
void sendCommand(TCPSocket * sock,int command);
void sendData(TCPSocket * sock,string data);
int readCommand(TCPSocket * sock);
string readData(TCPSocket * sock);
};
Project "MSA" has a class called "TCPMessengerServer" in which I’ve used "#include "MGeneralCommands.h"". For example, when trying to call the method "sendCommand" it won't recognize it.
From the comments, the error is actually something completely different:
‘sendData’ was not declared in this scope
from the code
sendData(socket,"TEST");
Unless you're already in a member function of MGeneralCommands (or a subclass), you'll need an object of that type to call it on:
commands.sendData(socket,"TEST");

How to add namespace in Unit Test Library(Windows store apps)?

I have some test class
using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Praktyka.Models;
namespace PraktykaTest
{
[TestClass]
public class PictureManagerTest
{
[TestMethod]
public void LoadImagesTest()
{
var pic = new PictureManager();
pic.LoadImages(new List<String>
{
"1.jpg",
"2.jpg"
});
// Assert.AreEqual(#"dataImages\1.jpg",pic.Current().UriSource);
Assert.AreEqual("test","test");
}
}
}
I have compile error
The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
and
Cannot initialize object of type 'List<string>' with a collection initializer
How to add reference for correct working with Lists ?
First, add a reference to the System.Collections.Generic namespace to get the generic List<> class. Then try recompiling.