C# :: Breaking Dependency In Static Method For Unit Test?
Feb 6, 2014
I have function that returns historical data. I can access it, using file name. If I use file name, it reads that file and saves it to dictionary, so that in the future, if historical data is required for the same file, it does not read it again (it's lazy loading). If no file is supplied to the function, it tries to read file which is given in app settings.
However, for unit testing, I do not want to read any file. Instead, I want it to use small sample of hardcoded historical data. In order to do that, I think, I need to introduce interface to it. Then I can use some IoC to choose between different implementation for unit testing purpose and ordinary launch of application.
Function to get history is given as follows:
public static class Auxiliary
{
private static Dictionary<string, MyData> _myData;
public static MyData GetData(string fileName = null)
{
// ...
}
}
I have created default Unit Test project with Visual Studio so, as far as I know, by default it uses MSTest as test runner and MSUnit as unit testing framework but it does not have any IoC container so I should manage NuGet packages for solution and install Unity.
As far as I know, MSUnit (aka Moles) can unit test static methods (it's unconstrained isolation framework, like Typemock Isolator, unlike NUnit) but still many people suggest not to use any static methods for unit testing.
Should I use shim or stub [URL] Stubs should be used for faking external dependencies and here it is not external library, but my own code.
View 1 Replies
ADVERTISEMENT
Jan 31, 2012
I am running a unit test on a library and this line keeps failing on Fedora 16, G++ 4.6.2
Code:
assertNAN(double, std::numeric_limits<double>::signaling_NaN()); //sanity check
The assert looks like this
Code:
//needs to copy it so that if its a function call it only does it once
#define assertNAN(type, one) {
type val = (type)one;
std::string lag(#one);
lag += " not a number";
[Code] ...
I am compiling with -DNDEBUG -O3 -ffast-math -fexpensive-optimizations to simulate a production environment. Is there a way to test for NAN consistently?
View 11 Replies
View Related
Apr 16, 2014
I'm new to c++ and boost library also. I need to test a function of my library. For example
// Functions.hpp
int add(const int x, const int y);
//Functions.cpp
int add(const int x, const int y)
{
return (x + y);
}
Now i need to test add function using boost. I need the result or output in below style. What all settings do i need to do in VS 2010 and how i should include boost test in the project.
==== Run unit tests ====
Running 2 test cases...
./mytest.cpp(13): error in "SimpleTestInMainTestingModule": check 1 == 2 failed
Test suite "Master Test Suite" failed with:
1 assertion out of 2 passed
1 assertion out of 2 failed
1 test case out of 2 passed
1 test case out of 2 failed
View 1 Replies
View Related
Feb 16, 2012
I am trying to build an application that has one static library dependency, however I am getting this error when linking:
1>ClCompile:
1> All outputs are up-to-date.
1>LINK : fatal error LNK1104: cannot open file 'TestWrapperLib.obj'
Why I might be getting that? I have the .lib in the depends line, and the directory where it is at in the include line.
View 1 Replies
View Related
Jan 17, 2013
I've been trying for more than one month to access a method found in a library called libcocosnas_static.a. I'm using Cocos2d-X version 2.0.4. The library has been used many times by my company to make games using cocos2d-1.0.1-x-0.12.0 without any problem.
This is what I've done:
1- I added the include paths of the library to both eclipse and Android.mk
2- Included the .h file using #include "NASPlatformUtil.h"
3- Added the libcocosnas_static.a file to the proj.androidobjlocalarmeabi folder
4- Added "LOCAL_WHOLE_STATIC_LIBRARIES += cocosnas_static" to the Android.mk file
5- Called the function using: NASPlatformUtil:: openUrl("http://xxx.xxx.com/");
I can right click on the function, click Open Declaration and get it without any problem, but the compiler keeps on giving me that dreaded error...
View 3 Replies
View Related
Aug 22, 2014
Have following code:
class Program
{
static void Main(string[] args)
{
[Code]....
My question according to what i just wrote:
1. Is that mean that Do() is only available for use by Dog itself because Dog is 'oryginal' Dog, and if i create new dogs - instances of oryginal Dog (dog1, dog2 ...) they cant access because Do is only available fo 'oryginal' one? Is that correct thinking?
2. If i would want to have something common (e.g value) for all dogs is that good way to create static field/method for Dog instead of non-static once then all instances of Dog would access Dog static member to get/change it? Just stupid example: static method GetAmountOfLegs() which return 4 Then all instances can take/call that value from Dog. Is that correct thinking?
View 2 Replies
View Related
Feb 17, 2013
what is static method?
why we can use static method inc#?
View 1 Replies
View Related
Apr 3, 2013
How to use static in a class, function and variable.
View 1 Replies
View Related
Apr 16, 2012
I declared a member method to a class in its header file and implemented it in the cpp file. When I build and run the project in XCode, everything works fine. When I try to do it with a makefile, I get undefined symbols linker errors.
parser.h
Code:
#ifndef ENGINE_SCRIPT_PARSER_H
#define ENGINE_SCRIPT_PARSER_H
#include <string>
#include "variable.h"
namespace ninja_game_engine {
[Code] ....
The exact makefile looks like this:
Code:
test:
g++
Code/Engine/Modules/Timer/timer.cpp
Code/Engine/Modules/list.cpp
[Code] ....
I'm pretty sure that there is a weird namespace gotcha that I'm unaware of that LLVM (default OSX compiler) is compensating for that g++ isn't. Or maybe something weird with the optimization? I want the tests running at that level to make sure everything that is volatile is declared as such.
Rest of the code located here: [URL] ....
View 3 Replies
View Related
Mar 1, 2013
I would like to avoid throwing things in constructors as much as possible.
Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.
View 14 Replies
View Related
Dec 4, 2014
I am writing my program on C++ language. I have one promblem. I need to set signal handler for my process. As the signal is related with the process on system level I have faced the problem.
My program consists several classes. They are connected together. But it doesn't metter in this case. The problem is that I need to access to member and methods of the class from my signal handler. For instance , I have class named Foo at it has some members and methods.
So from my handler I need to call its function and change members.
I understand that compiler should know that this class instances will exist during all program execution.
I have tried to set static member class Foo instance in another class , but this didn't solve the problem.
What is correct approach to do this. How to correctly implement signal handling in such case.
Here is example of my code
Code:
class MyContainer{
private:
std::vector<Foo> container;
public:
int removeFromContainer(Foo* aFoo) {
[Code] .....
View 4 Replies
View Related
Aug 7, 2014
What is dependency in Makefile.
Secondly I have made Makefile but cannot use make -f . How to use it?
Third what is make [OPTION] [TARGET]
View 4 Replies
View Related
Jan 23, 2014
I have ran into some sort of Circular dependency between two classes.
Forward declaration doesn't work as I'm allocating the memory for pointer array of Student and it requires the default constructor.
P.S I'm aware I haven't written the BIG 3, it's an incomplete code. Just want to know how to resolve this dependency.
class Student;
class Course {
char *name;
Student *s[3];
[Code].....
View 14 Replies
View Related
Feb 26, 2015
i just want to break the loop when user hit ENTER.But after that programm is stil working, i don't know why, becouse i have a code just like in book, i spend a lot of time with it.
int getinfo(student pa[], int n){
cout << "START" << endl;
int i = 0;
for(i; i < n; i++) {
cout << "Studetn name";
char name[SLEN];
[code].....
View 2 Replies
View Related
Mar 29, 2013
I have an exercise from my text that defines a StrBlob class, then a StrBlobPtr class to hold weak pointers to the StrBlobs. This is from C++ Primer (5th Edition) and coincidentally, the entire chapter is available on-line at here.
My problem is that the begin and end functions of StrBlob can't be defined until the entire StrBlobPtr class is defined. Forward declarations don't cut it, since begin and end need more than pointers.
The solution (if you also look at the errata for the book) seems to be to define StrBlob, leave begin and end undefined, then full define StrBlobPtr, and following that, finally define StrBlob::begin() and StrBlob::end().
Anyhow, the above works, as I show in the included code below - but it seems like a hack and messy. What would be the proper way to do this? My text may be obfuscating the issue in the pursuit of pedagogy.
Additionally, how would one separate StrBlob and StrBlobPtr into there own headers? I'd think it impossible, since the StrBlob would have to nestle an "#include "StrBlobPtr.hpp" in the center of it's own definition...?
Code:
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <stdexcept>
class StrBlobPtr; // Forward declaration
[Code]....
As I said, the above works (compiles, haven't -tested- it extensively) but it seems messy.
View 2 Replies
View Related
Mar 14, 2014
Code:
while(x==1){
for (i=0;i<j;i++)
{if (word1[i] == word2[i])
{prefix[i]= word2[i];
counter++;}
else
x=2;}
Basically after the 3rd run of the for loop, it encounters a contradiction. I want it to exit right there and then. Instead it continues to run the for loop. What can I do?
View 4 Replies
View Related
Aug 12, 2013
I'd like to know how I could break a float into individual bytes for binary use.
Sounds simple, but it seems as though the compiler doesn't realize everything is just a series of bytes.
View 8 Replies
View Related
Nov 7, 2013
Today I faced a problem where I had circular dependency in my template arguments. I was trying to make a class hierarchy similar to:
template<class BType>
class A_base {
public:
BType* getB();
};
[Code] .....
Basically I had objects that were of type A<B<A<B<...
Basically I have a tree like structure of heterogeneous types that must facilitate two-way interactions where A's can call B's and B's can call A's. This structure is useful in many contexts the difference is the methods A and B provide are different in each of these contexts. Instead of adding the getA and getB and all the other connectivity methods in every version of A and every version of B, I wanted to create a base class that managed this automatically.
Another piece of advice was break up your code so there is a forward-only and backwards-only dependent types. This is not a complete solution because the two cannot know about the other and this does not really facilitate arbitrary two-way communication (where A calls B then B calls A back). It also makes the code more complicated in that I have two sets of objects and interfaces.
So the solution was to make the template arguments specific to the things I wanted to be flexible. The connectivity interface of A_base and B_base should be constant. Hence that cannot be in the template parameter. It was merely the traits that I wanted to make flexible so... I came up with this solution:
#include <iostream>
template<class aTraitType,class bTraitType>
class A;
template<class aTraitType,class bTraitType>
class B;
[Code] ....
Now this compiles and works great. The problem is that aObj and bObj cannot call their opposite within a trait method because print() does not know anything about the connectivity. So the solution there was to make traits an abstract base class. Then magically everything works!
#include <iostream>
template<class aTraitType,class bTraitType>
class A_base;
template<class aTraitType,class bTraitType>
class B_base;
[Code] .....
So this outputs the following. Clearly there is two-way communication!
Class A is not connected to B
Class B is not connected to A
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class A at 0x7fff25d1aa10 reporting for duty
Class B at 0x7fff25d1aa00 reporting for duty
View 6 Replies
View Related
May 16, 2012
I'm get input from the user and then storing it in "s " and then trying make tokens, but it is not working, and after making tokens I'm counting them. Here is My Code
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(){
string s;
[Code] ....
Here is my Output
Enter text:
Entered Text is: This is String
token 1: This
token 2: is
token 3: String
token 4:
Number of tokens: 3
The problem is that i want to store the first two strings "This" and "is" into two variables and how to remove token 4: which is unnecessary .
View 5 Replies
View Related
May 16, 2014
I know that you're allowed to use a char pointer to access any object but are you allowed to inspect a char array with a different type, say an unsigned integer without breaking the strict aliasing rule? My understanding is that it's not legal and could lead to trouble with trap representations but I just wanted to make sure.
View 4 Replies
View Related
May 15, 2013
When compiling the source I get the following error
"pms_program.h:54:56: error: expected declaration specifiers or ‘...’ before ‘CourseType’"
the code you are looking for in pms_course.h ( there is a circular dependency between pms.h , pms_course.h and pms_program.h)
function prototype AppendProgram ( part of doubly linked list )
I have attached the full course code.
As much as I like I cannot change the structure of the startup code ...
please see attached zip file for the entire source code.
View 6 Replies
View Related
Feb 9, 2015
I'm trying to make sure my code is written in smaller modules, so my first step is to create my initialization process in and external file to load the necessary data from external sources and set up things like the content of drop down list boxes.
My first attempt failed to give me access to the combobox items add function so I moved that code back into the form1.h file:
Code:
public:
Form1(void) {
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
void AddDate(char *date, int ID)
{
this->comboBox1->Items->Add("line 1");
}
It compiles fine, but the call to it in my Initialize.cpp file
Code:
MarketView::Form1::AddDate("abs",1);
Gives error C2352: 'MarketView::Form1::AddDate' : illegal call of non-static member function
OK, so I change "void AddDate" to "static void AddDate" and now get the error that "static member functions do not have 'this' pointers" so I go back to the "MarketView::Form1::comboBox1" situation where there is no legal syntax after "Box1 to get me to Items->Add
I've been an old fashion programmer for over 47 years. It seems as is the concept of programming computers has changed from the concepts of logic to memorization of complex syntax.
There has to be a simple answer to do this other than to write thousands of lines of code in one Form1.h file. I refuse to believe that the new programming concepts will not allow you to write code in smaller more manageable modules.
What is the proper syntax for breaking up the larger file into more manageable chucks?
View 12 Replies
View Related
Apr 17, 2013
From my book:
"A static function might have this prototype:
static void Afunction(int n);
A static function can be called in relation to a particular object by a statement such as the following:
aBox.Afunction(10);
The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:
CBox::Afunction(10);
where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."
Why exactly cant Afunction access non-static members?
View 7 Replies
View Related
Sep 11, 2013
What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?
View 7 Replies
View Related
Feb 21, 2014
I am reading the book C Programming Language. On Structures, it says:
"The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members."
What does it mean assigning a structure as a unit?
View 12 Replies
View Related
Apr 3, 2013
How to Install CPPUnit in Solaris? I don't have gcc in Solaris and all the packages I have downloaded for CPPUnit needs gcc to be compiled.
View 4 Replies
View Related