我已经阅读了大多数与SO相关的问题(这里,这里和那里).最后一个问题提出了四种替代方法来生成调用静态方法单元可测试的代码.我想问一下我的具体情况:我们有一个"业务逻辑层"或"规则"项目,它包含45个静态类(没有状态,只有静态方法).而且,它们不易自行测试:大多数都访问数据库和文件系统.无论如何,它并没有那么糟糕:要访问数据库,他们使用某些Mapper类的唯一实例(所有Mappers都是Singletons).每当我尝试对某些东西进行单元测试时,我都会碰到这堵墙.最大的问题是这是非常非常重要的代码,应该非常仔细地计划对其进行更改.我的问题:我应该如何使这个单元更加可测试?我应该编写45个接口并使用依赖注入吗?即便如此,我如何存根/模拟Mappers?
PS:我一直在阅读Michael Feathers的"使用传统代码",所以欢迎直接引用(其他书籍也是:)
编辑:由于有些人说解决方案可能依赖于平台,我正在研究.NET(C#和一些VB.NET)
是否有正确的方法来创建私有静态javascript变量(和函数),无论您创建多少次都不会更改new Obj?
这是我尝试过的,它似乎工作:
var ObjClass = (function(){
var static_var = 0; //private static variable
var static_fn = function(){ return static_var; }; //private static function
return function(){
++static_var;
var thisNumber = static_var;
this.getThisNumber = function(){
return thisNumber;
}
this.getStaticNumber = static_fn; //making static fn public
}
})();
var obj1 = new ObjClass;
var obj2 = new ObjClass;
var obj3 = new ObjClass;
console.log(obj1.getThisNumber()); //output `1`
console.log(obj1.getStaticNumber()); //output `3`
console.log(obj2.getThisNumber()); //output `2`
console.log(obj2.getStaticNumber()); //output …Run Code Online (Sandbox Code Playgroud) 我刚刚用了半个小时来解决这个问题,我已经设法修复了我的代码,但是我并不完全理解发生了什么,并且想知道是否有人可以对它进行说明.
我有一个utils类型类,它包含一些静态字段(例如数据库连接端点),这些字段由各种其他程序使用,具体取决于手头的任务.基本上是一个图书馆.
这就是它之前的样子(虽然仍然破碎);
//DBUtils.java
public final class DBUtils {
private static DBConnection myDBConnection = spawnDBConnection();
private static DBIndex myDBIndex = null;
private static DBConnection spawnDBConnection() {
//connect to the database
//assign a value to myDBIndex (by calling a method on the DBConnection object) <- IMPORTANT
//myDbIndex NOT NULL HERE
System.out.println("database connection completed");
//return the DBConnection object
}
public static searchDB(String name) {
//use the myDBIndex to find a row and return it
}
}
Run Code Online (Sandbox Code Playgroud)
简而言之,我使用静态spawnDBConnection()方法为myDBConnection和myDBIndex …
虽然将实例方法monkeypatch到类很简单,例如
class A(object):
pass
def a(self):
print "a"
A.a = a
Run Code Online (Sandbox Code Playgroud)
与其他类的这样做@staticmethod点菜
class B(object):
@staticmethod
def b():
print "static b"
A.b = B.b
Run Code Online (Sandbox Code Playgroud)
导致A.b()产生一个
TypeError:b()必须使用A实例作为第一个参数调用unbound方法(没有任何东西)
我试图从类中的非静态函数访问静态成员,我得到一个错误说
无法从实例变量访问静态成员
这就是我的代码看起来的样子 -
class myClass {
public static testStatic: number = 0;
public increment(): void {
this.testStatic++;
}
}
Run Code Online (Sandbox Code Playgroud)
根据我对静态成员/方法的理解,我们不应该访问静态函数中的非静态成员,反之亦然.静态成员已经创建并且有效,那么为什么我不能从我的非静态方法访问?
根据我对C++的理解,下面的代码是错误的.
#include <iostream>
class Test {
private:
int num_;
public:
Test(int n) : num_(n) {}
void printNum() { std::cout << num_ << '\n'; }
void weird() { Test::printNum(); }
};
int main() {
Test t(10);
t.weird();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Test::weird()调用,Test::printNum()因为它将是一个静态成员函数.但是,Test::printNum()访问实例属性并且显然不是静态的.然而,代码编译并运行到输出10.
我的编译器是Apple LLVM版本7.0.0(clang-700.1.76)
我错过了什么?
我知道一定有办法做到这一点。但我收到错误“TypeError:第一个参数必须可调用”。
我可以采取哪些不同的措施来完成这项工作?
class FaxMachine(object):
MODEL_NO = '100'
@staticmethod
def load_fax(fax, error=''):
# send fax here
fail_fax = functools.partial(load_fax, error='PC LOAD LETTER')
Run Code Online (Sandbox Code Playgroud) 我不清楚使用静态方法设计类的最佳方法是什么,这些方法可以覆盖.我将尝试用一个例子来解释.
我们有Goat一个方法类can_climb.(顺便说一下,这是Python 3,在Python 2中我会写class Goat(object):.)
class Goat:
def __init__(self, *args):
...
def can_climb(self, mountain):
return mountain.steepness < 10
billy = Goat("Billy")
if billy.can_climb(mount_everest):
print("wow Billy, impressive")
Run Code Online (Sandbox Code Playgroud)
这按预期工作,但该方法can_climb不使用self.使它成为静态方法看起来更干净,而且pylint甚至会对上述方法发出警告.所以让我们改变一下:
class Goat:
def __init__(self, *args):
...
@staticmethod
def can_climb(mountain):
return mountain.steepness < 10
billy = Goat("Billy")
if billy.can_climb(mount_everest):
print("wow Billy, impressive")
Run Code Online (Sandbox Code Playgroud)
billy.can_climb接近结束可以代替,Goat.can_climb并且在这个例子中它不会有所作为.有些人甚至可能会认为通过类而不是实例调用静态方法更清晰,更直接.
但是,当我们使用继承并引入多态时,这会导致一个微妙的错误:
class Goat:
def __init__(self, *args):
...
@staticmethod
def can_climb(mountain):
return mountain.steepness < 10
class MountaineeringGoat(Goat):
@staticmethod
def …Run Code Online (Sandbox Code Playgroud) 我试图 classmethod用functools.lru_cache. 我的尝试失败了:
import functools
class K:
@functools.lru_cache(maxsize=32)
@classmethod
def mthd(i, stryng: str): \
return stryng
obj = K()
Run Code Online (Sandbox Code Playgroud)
错误消息来自functools.lru_cache:
TypeError: the first argument must be callable
Run Code Online (Sandbox Code Playgroud) python static-methods decorator python-3.x python-decorators
我正在为图像处理算法编写一个类,它有一些方法,特别是一些静态方法。我的 IDE 不断告诉我将静态方法转换为函数,这导致我提出以下问题:
什么时候应该将静态方法变成函数?什么时候不应该呢?
static-methods ×10
python ×5
.net ×1
c++ ×1
decorator ×1
function ×1
functools ×1
inheritance ×1
java ×1
javascript ×1
methods ×1
non-static ×1
object ×1
polymorphism ×1
python-3.x ×1
refactoring ×1
static ×1
typescript ×1
unit-testing ×1