我目前正在为一些遗留代码添加一些单元测试,我发现自己需要覆盖一个open函数.实时代码看起来像这样.
if ( !open( F, $filetoopen) ){
# do stuff with <F>
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是确保"F"包含我从测试中提供的文件句柄,而不是它认为它的开头.
我的.t文件中有以下代码...
BEGIN {
*CORE::GLOBAL::open = sub { open(F,$testfiletoopen); };
};
Run Code Online (Sandbox Code Playgroud)
...它确实有效,测试中的代码完成了从我的测试文件读取.但是,只要我使用与测试中的代码相同的文件句柄名称"F",它就会继续工作.
如果有办法使这个测试代码不那么脆弱,那么如果在实时代码中更改了文件句柄名称,那么测试不会失败?
谢谢
使用Spring进行单元测试的最佳实践方法是什么?我假设SpringN的组合TestNG和jmockit并不坏,所以这就是我现在正在做的事情,但如果我当然没有为我的新Spring项目选择工具,请马上告诉我.:-)
无论如何,我已经创建了一个我想测试的实体,但我不确定如何从Spring上下文中实际运行TestNG.我创建了一个简单的测试类
package com.mydomain.data.entities.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.*;
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class SimpleTest extends AbstractTestNGSpringContextTests {
@Autowired
private ApplicationContext applicationContext;
@BeforeClass
protected void setUp() throws Exception {
Assert.assertNotNull(applicationContext);
}
@Test
public void testNothing() {
}
}
Run Code Online (Sandbox Code Playgroud)
使用applicationContext.xml导入bean以设置模型和业务层.首先,我希望它使用Project/WebRoot/WEB-INF中的applicationContext.xml,而此测试的源代码位于Project/src/com/mydomain/data/entities/test中,但我相信/applicationContext.xml会给我Projects/src,和classpath一样:这是正确的吗?
此外,既然我正在使用我的Web应用程序将使用的模型和业务层,我应该期望它的行为类似.但是,当我在测试中启动TestNG时,它是sais:class org.hibernate.cfg.ExtendedMappings将接口org.hibernate.cfg.Mappings作为超类:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/Users/niklas/Documents/Eclipse/Project/WebRoot/WEB-INF/Project-Model.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1393)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我NSDictionary的键应该是子类的实例NSManagedObject.
然而,问题是NSManagedObject没有实现NSCopying协议,这意味着没有Core Data对象/实例NSManagedObject可以用作字典键,即使该-[hash]方法适用于它们.
我该怎么办?
cakephp中的表单助手
echo $form->input('Firstname', array('class'=>'test','name'=>'Firstname','type'=>'text'));
Run Code Online (Sandbox Code Playgroud)
生成我像下面这样
<div class="input text">
<label for="Firstname">Frrstname</label>
<input type="text" id="Firstname" value="" class="test" name="Firstname"/>
</div>
Run Code Online (Sandbox Code Playgroud)
是否可以在DIV中添加标签和输入之间的间隔..如果是这样,请建议
我知道这不起作用,但希望你能看到我正在努力做的事情
#if ASSIGN_ALLOWED
#define MAYBE_SKIP_REST_OF_LINE
#else
#define MAYBE_SKIP_REST_OF_LINE ; //
#endif
char str[80] MAYBE_SKIP_REST_OF_LINE = "Hello\n";
long array[3] MAYBE_SKIP_REST_OF_LINE = { 7,8,9 };
int x MAYBE_SKIP_REST_OF_LINE = 3;
//...many many more similar lines...
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,它的工作原理?
我SerializationException: (...) is not marked as serializable.在以下代码中收到错误:
[Serializable]
public class Wind
{
public MyText text;
public Size MSize;
public Point MLocation;
public int MNumber;
/.../
}
[Serializable]
public class MyText
{
public string MString;
public Font MFont;
public StringFormat StrFormat;
public float MySize;
public Color FColor, SColor, TColor;
public bool IsRequest;
public decimal MWide;
/.../
}
Run Code Online (Sandbox Code Playgroud)
和要序列化的列表:
List<Wind> MyList = new List<Wind>();
Run Code Online (Sandbox Code Playgroud)
FileStream FS = new FileStream(AppDomain.CurrentDomain.BaseDirectory +
"Sticks.dat", FileMode.Create);
BinaryFormatter BF = new BinaryFormatter();
BF.Serialize(FS, MyList); …Run Code Online (Sandbox Code Playgroud) 从第1天开始,我一直在努力解决这个问题.我可能已经被很多甚至根本不处理错误的代码所包围.
无论如何,我正在使用传统的n层设计中的WebForms:UI-> BLL-> DAL.通常我所做的(我知道它不对)是尝试/捕获我的数据操作.如果有例外我只是把它扔到泡沫中.
try
'db operations
catch ex as exception
throw
finally
'close connections
end
Run Code Online (Sandbox Code Playgroud)
那么它会冒泡到BLL,并且还有另一个try/catch,我将记录错误.现在我想提醒用户有什么问题,所以我再次抛出它,这样它就会冒泡到用户界面.在UI级别,我将包装一个try/catch,如果有错误,我将向他们显示一条友好的消息.
你的想法是什么?我能在这做些什么?
我已经在 IIS 服务器上部署了一个应用程序,并配置了 Servlet Exec。我需要使用 Windows 用户凭据而不是匿名用户运行应用程序。我尝试在属性/安全性中更改匿名用户的用户名和密码,但应用程序停止响应将其更改回默认匿名 IIS 用户帐户工作正常吗?
阿卜杜勒·哈利克
无论如何要做这样的事情:
class A:
def foo(self):
if isinstance(caller, B):
print "B can't call methods in A"
else:
print "Foobar"
class B:
def foo(self, ref): ref.foo()
class C:
def foo(self, ref): ref.foo()
a = A();
B().foo(a) # Outputs "B can't call methods in A"
C().foo(a) # Outputs "Foobar"
Run Code Online (Sandbox Code Playgroud)
调用者在哪里A使用某种形式的内省来确定调用方法对象的类?
编辑:
最后,我根据一些建议把它放在一起:
import inspect
...
def check_caller(self, klass):
frame = inspect.currentframe()
current = lambda : frame.f_locals.get('self')
while not current() is None:
if isinstance(current(), klass): return True
frame = frame.f_back …Run Code Online (Sandbox Code Playgroud)