我正在使用 Check 框架对我的 C 代码进行单元测试,但我找不到测试静态方法的正确方法。
我的工作根本不理想,希望有人能指出我正确的方向,如何正确地做到这一点。我的解决方法是添加 #ifdef 宏,将静态方法更改为 extern,以防我在编译时传递 -D DEBUG。
在源文件中
#ifdef DEBUG
unsigned ds_roundup_to_prime (const unsigned bsize) {
#else
static inline unsigned ds_roundup_to_prime (const unsigned bsize) {
#endif
Run Code Online (Sandbox Code Playgroud)
在头文件中我这样做
#ifdef DEBUG
unsigned ds_roundup_to_prime (const unsigned bsize);
#endif
Run Code Online (Sandbox Code Playgroud)
理想情况下,不应更改源代码来适应单元测试。单元测试框架应该能够测试源代码,就像它在生产中的样子一样。
谢谢
我正在我的 flutterfire 项目中准备一个类,我想使用一些无法进一步更改的方法,以便我想知道 Dart 中 static 关键字的概念?
我有一个具有静态方法的类,我想在该类中使用另一个静态方法来调用该方法,但它返回NameError: name ''method_name' is not defined
我正在尝试做的事情的例子。
class abc():
@staticmethod
def method1():
print('print from method1')
@staticmethod
def method2():
method1()
print('print from method2')
abc.method1()
abc.method2()
Run Code Online (Sandbox Code Playgroud)
输出:
print from method1
Traceback (most recent call last):
File "test.py", line 12, in <module>
abc.method2()
File "test.py", line 8, in method2
method1()
NameError: name 'method1' is not defined
Run Code Online (Sandbox Code Playgroud)
解决这个问题的最佳方法是什么?
我想将代码保留为这种格式,其中有一个类包含这些静态方法并使它们能够相互调用。
例如,我有两个类:Foo和Bar.这些类映射到一些表.
至于现在,我为每个类都有静态方法:Add,Update,Delete,Get.
E.g.:
public class Foo
{
private Guid _id;
private string _someProperty;
static Foo Get(Guid id);
static void Add(Foo foo);
static void Update(Foo foo);
static void Delete(Foo foo);
}
Run Code Online (Sandbox Code Playgroud)
所以,当我需要用我的对象做smth时我会这样说:
Foo foo = Foo.Get(id);
Foo newfoo = new Foo();
Foo.Add(newfoo);
Foo.Update(newfoo);
Foo.Delete(newfoo);
Run Code Online (Sandbox Code Playgroud)
这是一个好方法吗?如果不是,我应该使用什么方法来访问数据?
谢谢
虽然这是一个相当普遍的问题,但我正在努力解决它的最佳方法(如果在这种情况下需要接近它).
I have inherited a website (ASP.NET, C#) part of which contains a class full of static methods (it's a very large class, honestly). One method in particular is for sending e-mails. It has every possible parameter I can think of and it works well enough. However, the internals of that particular method are rather cumbersome to manage and understand due to the fact that everything is shoved inside - particularly when most of the parameters aren't used. In addition, …
我真的很喜欢使用静态方法(特别是对于helpers类).但是由于静态方法不是顽固的,最终它们是一种不好的做法,不是吗?所以我必须在静态方法使用方便性和可测试性之间做出选择.有任何妥协吗?
我有一个类,我想通过创建它的静态实例作为单例使用.当然,我也希望它是线程安全的.
假设我没有共享任何私人数据.但是如果我没有弄错的话,仍然存在这样的问题:当调用静态对象实例的方法时,方法中的变量在线程之间共享,并且会产生不可预测的结果.
但是,在调用真正的静态方法时,会创建一个新的堆栈帧,因此无论如何它都是线程安全的(自身).如果我没有弄错的话.
单身人士的这种模式是否是线程安全的?
class Singleton
{
public object SomeMethod(object arg) {
return Singleton.SomeMethodImpl(arg);
}
private static object SomeMethodImpl(object arg) {
// is in unique stack frame?
object Result;
...
return Result;
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想知道我为什么不首先创建一个静态类 - 我需要一个基于接口的单例,并且具有不同的实现,作为策略模式的一部分.这不适用于静态类.
我正在编写一个代码来处理C++中的向量.我有3个文件:main.cpp,Vektor.cpp和Vektor.h现在我想在main中调用一个静态funktion,它在Vektor.cpp中实现并在Vektor.h中声明."test"和"test2"是Vektor类的两个实例.Eclipse抛出一个错误,但我不知道为什么; 它说
此行的多个标记 - 无法解析函数'addieren' - 此范围内未声明'addieren' - 'endl'的无效重载 - 行断点:main.cpp [line:28]
哪里出错了?包括"Vektor.h".以下是必要的插条:
main.cpp中:
// ...
cout << "Summe: " << addieren(test,test2) << endl;
Run Code Online (Sandbox Code Playgroud)
Vektor.cpp:
Vektor Vektor::addieren(Vektor vektor1, Vektor vektor2)
{
Vektor vektorSumme;
vektorSumme.set_x(vektor1.get_x() + vektor2.get_x());
vektorSumme.set_y(vektor1.get_y() + vektor2.get_y());
vektorSumme.set_z(vektor1.get_z() + vektor2.get_z());
return vektorSumme;
}
Run Code Online (Sandbox Code Playgroud)
Vektor.h:
class Vektor
{
//...
public:
//...
static Vektor addieren(Vektor vektor1, Vektor vektor2);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!!
我有一个包含类型名称的字符串.我想在反射中获取类型,并调用静态方法.我想尽可能简化代码.这样的事情:
public class MyClass {
static int foo()
{
return 7;
};
}
var MyClassType = Type.GetType("MyClass");
// your help here!
int res = (MyClassType).foo();
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在尝试使用gridview(http://www.rogcg.com/blog/2013/11/01/gridview-with-auto-resized-images-on-android)和mediaplayer(http:/ /examples.javacodegeeks.com/android/android-mediaplayer-example/)试图结合我发现的两个教程,这样就可以从GridView中选择一个图像,启动一个播放歌曲的MediaPlayer活动.
GridView教程建议这样做以启用单击:
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// this 'mActivity' parameter is Activity object, you can send the current activity.
Intent i = new Intent(MainActivity.this, AndroidMediaPlayer.class);
MainActivity.startActivity(i);
}
});
Run Code Online (Sandbox Code Playgroud)
但我无法解决问题并得到警告:
Non-static method 'startActivity(android.content.Intent)' cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud) static-methods ×10
c# ×5
class ×2
android ×1
asp.net ×1
c ×1
c++ ×1
dart ×1
flutter ×1
gridview ×1
methods ×1
nhibernate ×1
python ×1
reflection ×1
singleton ×1
static ×1
unit-testing ×1