我很困惑他们之间有什么区别,在哪种情况下选择哪一个.一些差异可能是显而易见的,比如any和eq,但我把它们都包括在内只是为了确定.
我想知道他们之间的差异,因为我遇到了这个问题:我在Controller类中有这个POST方法
public Response doSomething(@ResponseBody Request request) {
return someService.doSomething(request);
}
Run Code Online (Sandbox Code Playgroud)
并希望在该控制器上执行单元测试.我有两个版本.第一个是简单的,就像这样
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(req)).thenReturn(res);
Response actualRes = someController.doSomething(req);
assertThat(actualRes, is(res));
}
Run Code Online (Sandbox Code Playgroud)
但我想使用像这样的MockMvc方法
@Test
public void testDoSomething() {
//initialize ObjectMapper mapper
//initialize Request req and Response res
when(someServiceMock.doSomething(any(Request.class))).thenReturn(res);
mockMvc.perform(post("/do/something")
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(req))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$message", is("done")));
}
Run Code Online (Sandbox Code Playgroud)
两者都运作良好.但我希望我someServiceMock.doSomething()在MockMvc方法中接收req,或者至少是一个具有相同变量值的对象req(不仅仅是任何Request类),并返回res,就像第一个一样.我知道使用MockMvc方法是不可能的(或者是吗?),因为实际调用中传递的对象总是与mock中传递的对象不同.无论如何我能做到吗?或者这样做甚至有意义吗?或者我应该满意使用any(Request.class)?我试过了eq,same但是所有这些都失败了.
先感谢您.我希望我能很好地解释自己.
我正在使用一个while(matcher.find())循环遍历模式的所有匹配.对于它找到的那个模式的每个实例或匹配,我想matcher.group(3)用一些新文本替换.这个文本对于每个文本都是不同的,所以我用它matcher.appendReplacement()来重建原始字符串并进行新的更改.但是,appendReplacement()替换整个模式而不仅仅是组.
我怎么能这样做但只修改匹配的第三组而不是整个模式?
这是一些示例代码:
Pattern pattern = Pattern.compile("THE (REGEX) (EXPRESSION) (WITH MULTIPLE) GROUPS");
Matcher matcher = pattern.matcher("THE TEXT TO SEARCH AND MODIFY");
StringBuffer buffer = new StringBuffer();
while(matcher.find()){
matcher.appendReplacement(buffer, processTheGroup(matcher.group(3));
}
Run Code Online (Sandbox Code Playgroud)
但我想做这样的事情(显然这不起作用).
...
while(matcher.find()){
matcher.group(3).appendReplacement(buffer, processTheGroup(matcher.group(3));
}
Run Code Online (Sandbox Code Playgroud)
像这样的东西,它只取代某个组,而不是整个模式.
编辑:更改了正则表达式示例,以显示并非所有模式都已分组.
我正在 Flutter 中对渲染对象进行测试。我想检查这样的不平等(简化):
testWidgets('render object heights not equal', (WidgetTester tester) async {
final renderObjectOneHeight = 10;
final renderObjectTwoHeight = 11;
expect(renderObjectOneHeight, notEqual(renderObjectTwoHeight));
});
Run Code Online (Sandbox Code Playgroud)
我编造了,notEqual因为它不存在。这也不起作用:
!equals我找到了一个有效的解决方案,所以我在问答风格下发布了我的答案。不过,我欢迎任何更好的解决方案。
我知道regEx在各种语言中很常见......但是我在编写Java语法时遇到了麻烦.我有一个用JS编码的正则表达式;
if((/[a-zA-Z]/).test(str) && (/[0-9]|[\x21-\x2F|\x3A-\x40|\x5B-\x60|\x7B-\x7E]/).test(str))
return true;
Run Code Online (Sandbox Code Playgroud)
我如何在Java中编写相同的内容?
我已经进口了
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Run Code Online (Sandbox Code Playgroud)
只是添加,从我尝试它是说\ x是一个无效的转义字符..
假设a Regular Expression,通过Java Matcher对象与大量字符串匹配:
String expression = ...; // The Regular Expression
Pattern pattern = Pattern.compile(expression);
String[] ALL_INPUT = ...; // The large number of strings to be matched
Matcher matcher; // Declare but not initialize a Matcher
for (String input:ALL_INPUT)
{
matcher = pattern.matcher(input); // Create a new Matcher
if (matcher.matches()) // Or whatever other matcher check
{
// Whatever processing
}
}
Run Code Online (Sandbox Code Playgroud)
在Java SE 6 JavaDoc for Matcher中,可以Matcher通过该reset(CharSequence)方法找到重用同一对象的选项,正如源代码所示,该方法比Matcher每次创建新对象要便宜一些,即,与上面不同,可以做: …
我非常喜欢RSpec能够分离控制器和视图测试的方式,但是在使用capybara匹配器在视图测试中工作时会遇到一些问题.我基本上试图实现的是这样的:
describe "some page" do
it "should render with lots of stuff" do
assign ..
render
rendered.should have_button ('Any button') #or any capybara matcher, really
end
end
Run Code Online (Sandbox Code Playgroud)
我在网上看到一些帖子显示如何配置capybara和rails3以便与黄瓜或rspec控制器测试一起顺利工作,但这不是我想要的 - 也就是说,可能在最低级别测试视图.
另外,如果有另一种方法可以做到这一点(不需要大量的自定义代码,我知道我可以编写一些匹配器,从使用nokogiri或任何合适的工具提取给定的选择器),这也很棒 - 使用水豚不是必需的.
在Java中我有:
String params = "depCity=PAR&roomType=D&depCity=NYC";
Run Code Online (Sandbox Code Playgroud)
我想获得depCity参数值(PAR,NYC).
所以我创建了正则表达式:
String regex = "depCity=([^&]+)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(params);
Run Code Online (Sandbox Code Playgroud)
m.find()是假的.m.groups()正在恢复IllegalArgumentException.
我究竟做错了什么?
我经常进行单元测试,我需要比较两个矩对象。我希望我们使用 moment 的内置函数 moment.isSame(moment) 来比较它们。但是,这意味着我的断言将如下所示:
expect(moment1.isSame(moment2)).toBeTrue();
我不太喜欢这个,特别是因为失败消息的信息量会较少。因此,我想编写一个自定义的笑话匹配器“toBeSameMoment”。以下代码似乎至少可以编译:
import moment from "moment";
declare global {
namespace jest {
interface MomentMatchers extends Matchers<moment.Moment> {
toBeSameMoment: (expected: moment.Moment) => CustomMatcherResult;
}
}
}
expect.extend({
toBeSameMoment(received: moment.Moment, expected: moment.Moment): jest.CustomMatcherResult {
const pass: boolean = received.isSame(expected);
const message: () => string = () => pass ? "" : `Received moment (${received.toISOString()}) is not the same as expected (${expected.toISOString()})`;
return {
message,
pass,
};
},
});
Run Code Online (Sandbox Code Playgroud)
但是,我无法真正让它在我的单元测试中工作......当我尝试以下测试代码时:
import moment from "moment";
import "../jest-matchers/moment";
describe("Moment matcher", …Run Code Online (Sandbox Code Playgroud) 我可以检查 cypress 中是否存在文本cy.contains('hello'),但现在我从页面中删除 hello,我想检查 hello 不存在,我该怎么做cy.notContains('hello')?
目前,我有一个函数,有时会返回一个内部有一些函数的对象.使用expect(...).toEqual({...})它时似乎不匹配那些复杂的对象.具有函数或File类的对象(来自输入类型文件),它就是不能.怎么克服这个?
matcher ×10
java ×5
regex ×4
javascript ×2
append ×1
capybara ×1
cypress ×1
dart ×1
difference ×1
flutter ×1
jasmine ×1
jestjs ×1
mockito ×1
momentjs ×1
reset ×1
rspec ×1
testing ×1
textmatching ×1
ts-jest ×1
typescript ×1
unit-testing ×1