目前,我有一个函数,有时会返回一个内部有一些函数的对象.使用expect(...).toEqual({...})它时似乎不匹配那些复杂的对象.具有函数或File类的对象(来自输入类型文件),它就是不能.怎么克服这个?
所以我一直在使用EasyMock的类扩展.突然间我得到了这个异常,但只有当我运行整个测试套件时:
java.lang.IllegalStateException: 0 matchers expected, 1 recorded.
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:42)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:34)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:26)
at org.easymock.internal.RecordState.invoke(RecordState.java:64)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:24)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:56)
at org.easymock.classextension.internal.ClassProxyFactory$1.intercept(ClassProxyFactory.java:74)
at com.protrade.soccersim.data.emulator.matrix.PositionCategoryMatrix$$EnhancerByCGLIB$$c5298a7.getPossession(<generated>)
at com.protrade.soccersim.data.emulator.stats.team.PossessionCalculatorUnitTest.testDeterminePossessionHomeWin(PossessionCalculatorUnitTest.java:45)
Run Code Online (Sandbox Code Playgroud)
涉及的代码是这个小美女(修剪了一下):
@Before
public void setUp() throws Exception {
homeTeam = createMock( PositionCategoryMatrix.class );
awayTeam = createMock( PositionCategoryMatrix.class );
...
}
@Test
public void testDeterminePossessionHomeWin() {
expect(homeTeam.getPossession()).andReturn( 0.15151515 );
expect(awayTeam.getPossession()).andReturn( 0.01515152 );
replay( homeTeam, awayTeam );
...
}
Run Code Online (Sandbox Code Playgroud)
第一个期望是抛出异常.它真的没有意义.它说这是一个匹配器,但该方法甚至没有争论.奇怪的是,这只是在测试套件中!我正在@Before中创建一个新的模拟,所以它不应该从其他地方继承任何东西(不是其他方法会有一个匹配器)
那么,有什么想法吗?
我遇到了EasyMock 2.5.2和JUnit 4.8.2(通过Eclipse运行)的问题.我在这里阅读了所有类似的帖子,但没有找到答案.我有一个包含两个测试相同方法的测试的类.我正在使用匹配器.
以下是测试代码的简化版本:
private Xthing mockXthing;
private MainThing mainThing;
@Before
public void setUp() {
mockXthing = EasyMock.createMock(Xthing.class);
mainThing = new MainThing();
mainThing.setxThing(mockXthing);
}
@After
public void cleanUp() {
EasyMock.reset(mockXthing);
}
@Test
public void testTwo() {
String abc = "abc";
EasyMock.expect(mockXthing.doXthing((String) EasyMock.anyObject())).andReturn(abc);
EasyMock.replay(mockXthing);
String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());
assertEquals("abc", testResult);
EasyMock.verify(mockXthing);
}
@Test
public void testOne() {
String xyz = "xyz";
EasyMock.expect(mockXthing.doXthing((String) EasyMock.anyObject())).andReturn(xyz);
EasyMock.replay(mockXthing);
String testResult = mainThing.testCallingXthing((Long) EasyMock.anyObject());
assertEquals("xyz", testResult);
EasyMock.verify(mockXthing);
}
Run Code Online (Sandbox Code Playgroud)
第二次(或最后一次)测试总是失败,并出现以下错误:
java.lang.IllegalStateException: 1 …Run Code Online (Sandbox Code Playgroud) 未来读者请注意:认为RSpec不认为您的哈希值相等?一个可能是OrderedHash,但是从常规RSpec输出你无法分辨.这是提示这篇文章的问题.
原始问题:
假设我有一个规范,我想测试一个方法生成适当的哈希.
it 'should generate the Hash correctly' do
expected = {:foo => 1, 'baz' => 2}
subject.some_method_that_should_generate_the_hash.should == expected
end
Run Code Online (Sandbox Code Playgroud)
这通常会失败,因为具有相同键值对的不同哈希可能会以不同的顺序返回它们的对.结果如下:
Failure/Error: subject.some_method_that_should_generate_the_hash.should == expected
expected: {:foo => 1, 'baz' => 2},
got: {'baz' => 2, :foo => 1}
Run Code Online (Sandbox Code Playgroud)
对于数组,使用=〜运算符求解.但是,这对Hashes不起作用.现在,我已经诉诸
it 'should generate the Hash correctly' do
expected = {:foo => 1, 'baz' => 2}
subject.some_method_that_should_generate_the_hash.each {|k,v|
v.should == expected[k]
}
end
Run Code Online (Sandbox Code Playgroud)
但这似乎不必要地冗长.我希望有一个明显的解决方案.我是否忽略了文档中的某些内容,或者RSpec没有适当的Matcher来进行无序的Hash平等?
使用新的expect语法:
expect(@line.filter_results_and_display_them).to == @processed
Run Code Online (Sandbox Code Playgroud)
得到此错误:
ArgumentError:expect语法不支持运算符匹配器,因此必须将匹配器传递给'#to'
Jest 的toEqual匹配器在检查相等性时会考虑空格。在测试中格式化预期值时,不可能以匹配包含换行符、制表符等的字符串的方式进行格式化。
Jest 是否提供了一种在匹配时忽略空格的方法?
注意:我编辑了问题以使其更通用。
我正在测试一个Matcher和Pattern类的小存根...请看下面的小存根..
package scjp2.escape.sequence.examples;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Sample_19 {
public static void main(String a[]){
String stream = "ab34ef";
Pattern pattern = Pattern.compile("\\d*");
//HERE * IS GREEDY QUANTIFIER THAT LOOKS FOR ZERO TO MANY COMBINATION THAT
//START WITH NUMBER
Matcher matcher = pattern.matcher(stream);
while(matcher.find()){
System.out.print(matcher.start()+matcher.group());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里......我们比较的字符串是"ab34ef".长度为6.
Noe让我们看看迭代......
1 0""
2 1""
3 2 34
4 4""
5 5""
现在..let结合... matcher.start()+ matcher.group()....根据我们的计算输出是:0123445
但是,存根生成01234456.
我无法理解"6"的来源.字符串索引从零开始,所以这里可以有最大索引是5.So从哪里来的?
它循环遍历循环六次......怎么样?有什么建议吗?
我想编写一个测试来断言给定对象不具有某些属性。
说我有一个函数
function removeFooAndBar(input) {
delete input.foo;
delete input.bar;
return input;
}
Run Code Online (Sandbox Code Playgroud)
现在我想写一个测试:
describe('removeFooAndBar', () => {
it('removes properties `foo` and `bar`', () => {
const data = {
foo: 'Foo',
bar: 'Bar',
baz: 'Baz',
};
expect(removeFooAndBar(data))
.toEqual(expect.objectContaining({
baz: 'Baz', // what's left
foo: expect.not.exists() // pseudo
bar: undefined // this doesn't work, and not what I want
}));
});
});
Run Code Online (Sandbox Code Playgroud)
断言这一点的正确方法是什么?
目前我正在努力在java中转换具有等效字符的HTML代码.我需要将以下代码转换为字符.
è - è
® - ®
& - &
ñ - ñ
& - &
Run Code Online (Sandbox Code Playgroud)
我尝试使用正则表达式模式
(&#x)([\\d|\\w]*)([\\d|\\w]*)([\\d|\\w]*)([\\d|\\w]*)(;)
Run Code Online (Sandbox Code Playgroud)
当我调试时,matcher.find()给我true但控件跳过我编写转换代码的循环.不知道那里发生了什么.
另外,有没有办法优化这个正则表达式?
任何帮助表示赞赏.
例外
java.lang.NumberFormatException: For input string: "x26"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at org.apache.commons.lang.Entities.unescape(Entities.java:683)
at org.apache.commons.lang.StringEscapeUtils.unescapeHtml(StringEscapeUtils.java:483)
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个Hamcrest匹配器来单元测试返回java.util.Optional类型的方法.就像是:
@Test
public void get__Null(){
Optional<Element> element = Element.get(null);
assertThat( sasi , isEmptyOptional());
}
@Test
public void get__GetCode(){
Optional<Element> element = Element.get(MI_CODE);
assertThat( sasi , isOptionalThatMatches(allOf(hasproperty("code", MI_CODE),
hasProperty("id", notNullValue())));
}
Run Code Online (Sandbox Code Playgroud)
是否有任何可用的实现抛出Maven存储库?