标签: assertion

JMock 意外调用

下面我只是尝试模拟一个名为 TestWrapper 的类并对其设置“允许”期望。然而,在设定期望时我遇到了错误。当使用 easymock 并只是设置期望时,这似乎不会发生

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;

import java.math.BigDecimal;

public class CustomerPaymentProgramConverterTest {

    TestWrapper paymentType;

    Mockery mockery = new JUnit4Mockery() {{
            setImposteriser(ClassImposteriser.INSTANCE);
    }};

    @Before
    public void setupMethod() {

      paymentType = mockery.mock(TestWrapper.class);

    }

    @Test
    public void testFromWebService() {

        mockery.checking(new Expectations() {{

                    //debugger throws error on the line below.
                    allowing(paymentType.getScheduledPaymentAmount());
                    will(returnValue(new BigDecimal(123)));
                    allowing(paymentType.getScheduledPaymentConfirmationNumber());
                    will(returnValue(121212L));
        }});

    }
}
Run Code Online (Sandbox Code Playgroud)

TestWrapper.class

 //Class I am mocking using JMock
 public class TestWrapper {

     public  java.math.BigDecimal getScheduledPaymentAmount() { …
Run Code Online (Sandbox Code Playgroud)

java junit jmock assertion

5
推荐指数
1
解决办法
2万
查看次数

org.junit.Assert 本身的 JUnit NoClassDefFoundError

运行此代码时出现以下错误:(ColorMatrixgetarray()返回 float[])

74 public void testHueShift() {
75     ColorMatrix cm1 = new ColorMatrix();
76     ColorMatrix cm2 = hueShift(0f);
77     assertArrayEquals(cm1.getArray(), cm2.getArray(), 0f);
78 }
Run Code Online (Sandbox Code Playgroud)

错误:

 ----- begin exception -----
 java.lang.NoClassDefFoundError: org.junit.Assert
    at com.Common.lib.test.ColorFilterFactoryTest.testHueShift(ColorFilterFactoryTest.java:77)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
 ----- end exception -----
Run Code Online (Sandbox Code Playgroud)

但项目构建路径有它:

在此输入图像描述

编辑
这是测试类类路径 在此输入图像描述

这是被测试的类(它是一个库项目) 在此输入图像描述

java junit unit-testing junit4 assertion

5
推荐指数
1
解决办法
1万
查看次数

如何使用 Chai-jQuery 的断言来测试 jQuery?

我正在使用 Mocha/Chai/Chai-jquery 来测试用 Jquery 编写的代码。尽管我在 HTML 中包含了 jquery 和 chai-jquery,但当我运行此测试时:

  it("is focused", function () { expect($("#movie")).should.have.focus();});
Run Code Online (Sandbox Code Playgroud)

使用 chai-jquery 的断言:

 (expect = require('chai-jquery').expect) 
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

 TypeError: expect(...).should.have.focus is not a function
Run Code Online (Sandbox Code Playgroud)

那么我如何设置它以使用 Chai-jquery 的断言,如此处所示?这是我的代码:

html:

     <body>
        <div id="mocha"></div> 
    <script src="node_modules/mocha/mocha.js"></script>
    <script src="node_modules/jquery/src/jquery.js"></script>
    <script src="node_modules/chai/chai.js"></script>
    <script src="node_modules/chai-jquery/chai-jquery.js"></script>

    <script>
        mocha.ui('bdd'); 
        mocha.reporter('html'); 
        var expect = chai.expect; 
    </script>

    <script src="script.js"></script>
    <script src="test.js"></script>
    <script>
        { mocha.run(); }
    </script>
     <input id="movie"/>
      </body>
Run Code Online (Sandbox Code Playgroud)

脚本.js:

    $(document).ready(function() {
    $("#movie").focus();
    //etc
    });
Run Code Online (Sandbox Code Playgroud)

测试.js:

var jsdom = require('jsdom');
var document = …
Run Code Online (Sandbox Code Playgroud)

jquery unit-testing mocha.js assertion chai

5
推荐指数
0
解决办法
1227
查看次数

Node.js 中的 AssertionError 定义在哪里?

我想让我的单元测试断言特定的函数调用会在预期时抛出 AssertionError,而不是根本抛出异常。断言库(expect)通过传入异常构造函数来支持这样的事情,但我似乎无法找到导出 AssertionError 构造函数的位置(如果有的话)。它是否只是一个内部类而不暴露给我们?该文档包含对其的大量引用,但没有链接。

我有一个超级hacky的方法:

let AssertionError;

try {
    const assert = require("assert");

    assert.fail();
}
catch (ex) {
    AssertionError = ex.constructor;
}
Run Code Online (Sandbox Code Playgroud)

但我希望有更好的方法。

unit-testing node.js assertion expect.js

5
推荐指数
1
解决办法
5364
查看次数

如何断言某个字符串至少包含 List &lt;String&gt; 中的一个值?

我正在使用 Java 和 AssertJ 测试一些 UI 功能。因此,当我从 UI 收到一些大字符串时,我应该验证该字符串是否至少包含一个来自List<String>. 做相反的事情很容易 - 验证列表是否至少包含一次某个字符串值,但这不是我的情况。我在标准方法中找不到解决方案。

public static final List<String> OPTIONS = Arrays.asList("Foo", "Bar", "Baz");

String text = "Just some random text with bar";
Run Code Online (Sandbox Code Playgroud)

我需要的是这样的:

Assertions.assertThat(text)
                .as("Should contain at least one value from OPTIONS ")
                .containsAnyOf(OPTIONS)
Run Code Online (Sandbox Code Playgroud)

java testing assertion assertj

5
推荐指数
1
解决办法
3064
查看次数

如何为 __getitem__() 调用执行 assert_has_calls?

代码:

from unittest.mock import MagicMock, call

mm = MagicMock()
mm().foo()['bar']

print(mm.mock_calls)
print()

mm.assert_has_calls([call(), call().foo(), call().foo().__getitem__('bar')])
Run Code Online (Sandbox Code Playgroud)

输出:

[call(), call().foo(), call().foo().__getitem__('bar')]

Traceback (most recent call last):
  File "foo.py", line 9, in <module>
    mm.assert_has_calls([call(), call().foo(), call().foo().__getitem__('bar')])
TypeError: tuple indices must be integers or slices, not str
Run Code Online (Sandbox Code Playgroud)

如何修复这个断言?

python mocking assertion python-unittest python-3.7

5
推荐指数
1
解决办法
1002
查看次数

如何使用 jUnit 5 断言检查异常消息是否以字符串开头?

我使用org.junit.jupiter.api.Assertions对象来断言抛出异常:

Assertions.assertThrows(
        InvalidParameterException.class,
        () -> new ThrowingExceptionClass().doSomethingDangerous());
Run Code Online (Sandbox Code Playgroud)

简单地说,抛出的异常dateTime在其消息中有一个可变部分:

final String message = String.format("Either request is too old [dateTime=%s]", date);
new InvalidParameterException(message);
Run Code Online (Sandbox Code Playgroud)

随着版本我用5.4.0Assertions提供三种方法检查异常被抛出:

它们都没有提供检查字符串是否另一个字符串开头的机制。最后 2 个方法只检查 String 是否相等。我如何轻松检查异常消息是否以 开头,"Either request is too old"因为在同一InvalidParameterException.


我很欣赏一种方法assertThrows?(Class<T> expectedType, Executable executable, Predicate<String> messagePredicate),其中谓词将提供抛出message的断言,并且当谓词返回时断言通过,true例如:

Assertions.assertThrows(
    InvalidParameterException.class,
    () -> …
Run Code Online (Sandbox Code Playgroud)

java junit exception assertion junit5

5
推荐指数
1
解决办法
6662
查看次数

“总是成功”绰绰有余?[乐]

在节下的语法文档中:

“永远成功”断言

我复制了那里提供的示例,并添加了代码来显示在解析机制的每个阶段生成的表:

use v6.d;

grammar Digifier {
    rule TOP { [ <.succ> <digit>+ ]+ }
    token succ   { <?> }
    token digit { <[0..9]> }
}

class Letters {
    has @!numbers;

    method digit ($/) { @!numbers.tail ~= <a b c d e f g h i j>[$/]; say '---> ' ~ @!numbers }
    method succ  ($/) { @!numbers.push: '!'; say @!numbers }
    method TOP   ($/) { make @!numbers[^(*-1)] }
}

say 'FINAL ====> ' ~ Digifier.parse('123 456 789', actions …
Run Code Online (Sandbox Code Playgroud)

grammar assertion raku

5
推荐指数
1
解决办法
62
查看次数

断言 Python 字典中所有值的数据类型的更好方法

我正在构建一个单元测试,断言/检查字典中的所有值是否具有相同的数据类型:float

Python版本3.7.4

假设我有四个不同的字典:

dictionary1: dict = {
    "key 1": 1.0,
}

dictionary2: dict = {
    "key 1": "1.0",
}

dictionary3: dict = {
    "key 1": 1.0,
    "key 2": 2.0,
    "key 3": 3.0
}

dictionary4: dict = {
    "key 1": "1",
    "key 2": "2",
    "key 3": 3
}
Run Code Online (Sandbox Code Playgroud)

以及这样的单元测试用例:

class AssertTypeUnitTest(unittest.TestCase):

    def test_value_types(self):
        dictionary: dict = dictionary
        self.assertTrue(len(list(map(type, (dictionary[key] for key in dictionary)))) is 1 and
                            list(map(type, (dictionary[key] for key in dictionary)))[0] is float)


if __name__ == "__main__":
    unittest.main() …
Run Code Online (Sandbox Code Playgroud)

python dictionary types unit-testing assertion

5
推荐指数
1
解决办法
7779
查看次数

在 C++20 中实现无预处理器的断言

C++ 知道assert()哪个允许运行时检查,根据NDEBUG.

我想使用编译器代码替换该宏并避免使用预处理器。我需要执行以下操作:

  • 如果表达式的计算结果为,则中断或终止false
  • 记录调用断言的代码行
  • NDEBUG放弃构建的检查和传递的表达式

中断/终止应用程序很容易。

在 C++20 中,std::experimental::source_location我可以使用它来获取断言的代码位置。

编译时条件可以使用requires或完成constexpr if

但是我不知道如何避免对表达式的求值。当myAssert(expression)作为函数实现时,我需要将表达式结果作为函数参数传递,这意味着即使该参数未在函数内部使用,它仍然会被计算。

C++20 有没有办法解决这个问题?

编辑:模板化示例:

template <typename T> requires (gDebug)
void assertTrue(const T& pResult, const std::experimental::source_location& pLocation) noexcept
{
   if (!static_cast<bool>(pResult))
   {
      // error handling
   }
}

template <typename T> requires (!gDebug)
void assertTrue(const T&) noexcept
{
}
Run Code Online (Sandbox Code Playgroud)

c++ preprocessor assertion c++20

5
推荐指数
1
解决办法
1376
查看次数