- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在测试使用库对象的 SortedSet 的类的行为(常规类不是接口(interface),因此我引入了 cglib-nodep)。当排序集有多个对象时,我需要测试类的行为。 Library 对象已通过以下方式进行模拟:
Library library = Mock()
Library library2 = Mock()
然后,我创建一个 TreeSet:
def libraries = [library, library2] as TreeSet
并调用被测系统方法:
sut.doStuff(libraries).
当我调试此测试时,我发现库是一个只有一个元素的 SortedSet。这似乎是 Spock 处理平等方式的结果,如下所示:
def "equality test"() {
expect:
library == library2
}
当我运行测试时通过了。有没有办法可以避免这种行为?
编辑:将=更改为==,因为我无法输入
最佳答案
做了一些研究。看一下以下一组测试(groovy 控制台脚本):
@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def "not comparable mocks are not equal"() {
given:
def a1 = Mock(A)
def a2 = Mock(A)
expect:
a1.hashCode() != a2.hashCode()
!a1.equals(a2)
!(a1 == a2)
}
def "comparable mocks are not equal"() {
given:
def a1 = Mock(AC)
def a2 = Mock(AC)
expect:
a1.hashCode() != a2.hashCode()
!a1.equals(a2)
!(a1 == a2)
}
def "cannot create TreeSet when POJOs are not comparable"() {
given:
def a1 = Mock(A)
def a2 = Mock(A)
and:
a1.hashCode() != a2.hashCode()
!a1.equals(a2)
!(a1 == a2)
when:
new TreeSet([a1,a2])
then:
def e = thrown(ClassCastException)
e.message.endsWith('cannot be cast to java.lang.Comparable')
}
def "there's a problem with Comparable Mocks"() {
given:
def a1 = Mock(AC)
def a2 = Mock(AC)
and:
a1.hashCode() != a2.hashCode()
!a1.equals(a2)
!(a1 == a2)
when:
def s = new TreeSet([a1,a2])
then:
s.size() == 2
}
def "with HashSet it works as expected"() {
given:
def a1 = Mock(AC)
def a2 = Mock(AC)
and:
a1.hashCode() != a2.hashCode()
!a1.equals(a2)
!(a1 == a2)
when:
def s = new HashSet([a1,a2])
then:
s.size() == 2
}
}
class A {}
class AC implements Comparable {
int compareTo(Object o) {
1 //whatever value may be here, it's not called
}
}
通常,当对象实现 Comparable 接口(interface)时会出现问题。
Comparable
这一事实。它失败。据我所知,这不是预期的行为。TreeSet
。预期行为。Comparable
模拟创建的 TreeSet
的大小预计为 2。它失败了,大小为 1。HashSet
时,满足第四个测试的条件。IMO,这不是与 spock 相关的问题。 Spock 使用 cglib
作为模拟对象,这是应该寻找解释的地方。
编辑
如果为模拟对象覆盖 compareTo()
方法,则效果很好:
@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')
import spock.lang.*
class Test extends Specification {
def "there's a problem with Comparable Mocks"() {
given:
def a1 = Mock(AC) {
compareTo(_) >> 3145
}
def a2 = Mock(AC) {
compareTo(_) >> 3146
}
and:
a1.hashCode() != a2.hashCode()
!a1.equals(a2)
!(a1 == a2)
when:
def s = new TreeSet([a1,a2])
then:
s.size() == 2
}
}
class AC implements Comparable {
int compareTo(Object o) {
1 //whatever value may be here, it's not called
}
}
关于unit-testing - Spock如何处理Mocked对象的相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25552604/
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
假设我在文件中具有以下命名导出 customer.ts export const saveDetails = ()=>{} export const loadDetails = ()=>{} 假设我在
我一直试图绕过dart的模拟库,但似乎我仍然不明白。 在我的库中,我有一个对外部资源的HTTP请求,我想以此模拟它不要一直依赖外部资源。 我的库中的主类如下所示: SampleClass(String
使用 postman 模拟服务器存在问题。它不响应具有路由参数的请求。例如,我的 uri 如下所示: PUT : {{server_url}}/order/{id} 但是当我以这种方式调用模拟服务器时
将任何转换器或空属性转换添加到 Jest 配置时,模拟无法正常工作。下面是简单的代码。 Jest 配置: "transform": { any regex: any transformer } 模块说
我最近一直在做一个项目,该项目已经开始变得相当依赖,并且一直在探索使用 AutoMocking 容器来清理我的测试并使其不那么脆弱的想法。 我听说过 TDD/BDD 纯粹主义者反对使用它们的论点,例如
有谁知道为什么 UsernameExists 不会返回 True。我的语法一定在某个地方搞砸了。 [TestMethod()] public void GenerateUsername
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我得到一个 Moq 对象以在连续调用方法时返回不同的值。这是通过此扩展方法完成的: public static void ReturnsInOrder(this ISetup setup, param
rhino-mocks stub 和这里的期望有什么区别:在我看来它们的行为完全相同? mockContext.Stub(x => x.Find()) .Return(new List()
我正在尝试模拟方法 extra_get() 的调用,该方法通常返回一个字典列表。据我从模拟docs了解,如果我想返回iterable,我应该设置side_effect参数。 client.extra_
在我的 CentOS 6.2 机器的/var/lib/mock 文件夹下,我可以看到目标构建操作系统中的所有初始应用程序。如果我想添加 JDK 作为额外的应用程序,我该怎么做?谢谢! 最佳答案 只需将
我正在寻找一种让 stub 的返回值取决于其输入的干净方法。 目前我正在使用以下方法,但效果不佳。 metadataLogic.Expect(x => x.GetMake(args.Vehicle1.
我正在设置调用构建和执行查询的方法的期望。我想询问所用参数的属性。这可能吗 using (mocks.Record()) { Expect.Call(connection.Retrieve(S
有没有人有一个使用 Rhino Mocks 和 NInject 的自动模拟容器的实现? 最佳答案 好的,我使用 Moq 集成作为起点自己构建了一个。这很简单。你需要这 3 个类: public cla
使用后@patch在我的装饰器上它不再起作用了。我想进行一个失败并引发异常的调用,以便我可以检查我的装饰器是否正在捕获此异常,并正在调用某个函数。 mock do_sth_in_db让它引发异常是很容
谁能告诉我为什么下面的测试没有失败? [Test] public void uhh_what() { var a = MockRepository.GenerateMock(); a.
我找不到 Moq 和 Rhino 的具体功能比较。所有的问题都是“你更喜欢哪个以及为什么”,或者“这是你如何在 rhino 中进行简单的模拟以及如何在最小起订量中完成的”。 我在任何地方都找不到深入的
我正在尝试模拟数据存储库对象,但在 MockRepository 上设置期望后,它每次都返回 null。我的代码如下: [测试] 公共(public)无效 GetById_NotNull() { 预
我是一名优秀的程序员,十分优秀!