我的 Robolectric 单元测试遇到问题。
我可以毫无问题地断言,当侦听器使用该方法时,单击启动了一个新活动startActivity( Intent )
但当使用该方法启动新活动时,Robolectric 似乎遇到了麻烦startActivityForResult(Intent, int):在代码中进行一些中断让我发现该活动尚未启动(只需更改方法即可startActivity( Intent )使断言通过)。
这正常吗?遗憾的是,因为我的应用程序的第一个 Activity 使用startActivityForResult(Intent, int).
有人成功通过这种启动活动的方式进行测试吗?
感谢您的帮助 ..
等待!——这个问题并不像听起来那么愚蠢。标题很简单。
我有一些调试代码来验证数据结构的正确性,还有一些断言检查此正确性,我只想在调试版本中启用它们:
{$ifdef DEBUG}
function Sorted : Boolean;
function LinearSearchByValue(const T : TType) : NativeInt;
{$endif}
Run Code Online (Sandbox Code Playgroud)
然后在一个方法中:
assert(Sorted);
Run Code Online (Sandbox Code Playgroud)
例如。
在我启用断言的调试版本中,一切都很好。然而,在我禁用断言的发布版本中,该行assert(Sorted);会导致编译器错误E2003 Undeclared identifier: 'Sorted'。完全正确,标识符没有声明,但是断言也被关闭,不应该被评估或生成任何代码。(尝试通过声明方法来欺骗断言,但没有实现会导致正常错误“不满意的前向或外部声明”。它显然也在寻找方法主体。)
这会导致一些混乱的代码,其中根本不应该存在于发布版本中的方法必须被声明并具有主体,以便编译断言,而断言被关闭。
在 Delphi 2010 中,如何声明仅存在于调试版本中的方法,并在断言中使用那些也应仅存在于调试版本中的方法?
更多信息:
我尝试用 包装方法声明{$ifopt C+},它检查断言是否打开。调用assert仍然失败,并出现“未声明的标识符”。
编译器选项肯定是断言被关闭。(我检查了 :))
我尝试将assert使用这些方法的调用包装为{$ifdef DEBUG}. 然而,这很混乱并且不应该是必需的。有一次,它让我担心即使在发布版本中也会编译断言,并且出于性能原因我根本不需要它们。(这没有发生 - 未生成断言代码。)
我当前的策略是始终声明这些方法,但在发布版本中构建 ifdef 方法主体并用虚拟结果填充它。这里的目标是编译所有断言,但方法的开销尽可能小,并且它们的返回值(如果它们实际上在发布版本中被调用)显然是错误的。
Delphi 中是否有与 C/C++ 风格的宏等效的东西,ASSERT(x)在发布版本中,宏将被简单地定义为空,导致编译器既看不到也不关心断言内的语句?这是在 C++ 中使用宏的少数干净方法(IMO)之一。
因此,虽然未生成断言,但它们已被编译。这又回到了我的问题:如何最好地混合仅调试方法和断言以及发布版本?
我无数次编写了在访问其内存或外部内存后生成分段错误的代码:std::vectorstd::string
std::string test{"hello!"};
std::cout << test[12] << std::endl;
Run Code Online (Sandbox Code Playgroud)
这是一个可以在非优化/调试构建中在运行时捕获的错误,只需简单断言的少量额外成本。(但由于我们正在构建没有-DNDEBUG和没有-O3我们不期望获得最大性能。)
有什么理由std::string::operator[]不这样实施吗?
标准是否禁止在库代码中使用断言?
char std::string::operator[](std::size_t i)
{
// `assert_with_message` only exists in debug mode
#ifndef NDEBUG
assert_with_message(i < this->size(),
"Tried to access character " + std::to_string(i)
+ " from string '" + *this + "' of size "
+ std::to_string(this->size()));
#endif
return data[i];
}
Run Code Online (Sandbox Code Playgroud)
-DNDEBUG在没有编译程序的情况下编译程序并在运行时看到类似此消息的内容将非常有帮助:
断言已触发:尝试从字符串“hello!”访问字符 12 尺寸为 6。
按 (0) 继续。
按 (1) 中止。
请注意,术语“断言”指的是开发/调试构建检查,应从发布/优化构建中完全删除/优化该检查。
我正在尝试验证对实现以下接口的 Moq 的方法调用,但它无法匹配调用。
我的单元测试(简化):
[Test]
public void ShouldDeleteComponent()
{
var mockDao = new Mock<IComponentDataAccess>();
Target.ComponentDao = mockDao.Object;
Target.Execute();
mockDao.Verify(x => x.Delete(It.IsAny<Component>()), Times.Once);
}
Run Code Online (Sandbox Code Playgroud)
我的模拟对象的接口:
public interface IComponentDataAccess : IDataAccess<Component>
{
int Delete(Component entity);
}
public interface IDataAccess<T> where T : IEntity
{
int Delete(T entity);
}
Run Code Online (Sandbox Code Playgroud)
最后,代码是如何在被测系统中实际调用的:
public override void Execute()
{
DeleteItem(ComponentDao, existingComponent);
}
Run Code Online (Sandbox Code Playgroud)
其中调用:
protected virtual void DeleteItem<T>(IDataAccess<T> dataAccess, T item) where T : IEntity
{
dataAccess.Delete(item);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,DAO 作为其基本接口传入。在验证时,它发现以下调用:
执行的调用:
IDataAccess`1.Update(blah.namespace.UserAccount)
当它试图匹配的调用是:
IUserAccountDataAccess.Update(blah.namespace.UserAccount)
有没有办法用 Moq 验证此方法调用?
我正在使用 JUnit 4.12,这是我目前对经常使用的以下 API 的理解:
但是,我无法弄清楚以下几件事:
我想不出更好的标题。
在下面的代码中,如果rollBackLogger是nil,第一个测试用例将失败,但所有其他测试用例将引发异常。
有没有办法避免这种情况,除了使用 if语句?
我相信这是单元测试非常常见的情况,应该有一些功能assert或其他方式来避免这种情况。
assert.NotNil(rollbackLogger)
assert.Equal("Action", rollBackLogger[0].Action)
assert.Equal("random path", rollBackLogger[0].FilePath)
Run Code Online (Sandbox Code Playgroud) 我正在使用 testcafe 进行 api 测试,但是我们的 api 需要登录。下面是我的代码。我可以很好地看到 json 响应。但我不确定如何在页面上断言。
import { Selector } from 'testcafe';
import Page from './page';
// Page model
const page = new Page();
const url = 'https://myexample.com';
const elementWithIdOrClassName = Selector(value => {
return document.getElementById(value) || document.getElementsByTagName(value);
});
fixture `Test`
.page(url + '/talent/career')
.beforeEach( async t => {
await t
.typeText(page.username, 'gvp50')
.typeText(page.password, 'password')
.click(page.login_button)
});
// Tests
test('Text typing basics', async t => {
await t
.navigateTo(url+'/api/learner/learning_items')
.expect(Selector('html')).contains('learning_items');
});Run Code Online (Sandbox Code Playgroud)
运行此代码后,Testcafe 挂起。我也试过 Selector('body') 但它不起作用。
预期输出和实际输出的内容相同,但我得到 org.opentest4j.AssertionFailedError
我试图用所有替换/n,System.lineSeparator()但我得到的输出是:“内容仅在行分隔符中存在差异”
@BeforeEach
public void setUpStreamsAndEmptyFile() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
// emptying file contents
try {
PrintWriter pw = new PrintWriter(filePath);
pw.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
@AfterEach
public void restoreStreams() {
System.setOut(originalOut);
System.setErr(originalErr);
}
@Test
public void executeCreateEventCommand() {
String expected = "\t Got it. I've added this task: " + System.lineSeparator() +
"\t [Event][?] Project Meeting (at: 28/08/19 1600 - 28/08/19 1800)" + System.lineSeparator() +
"\t Now you …Run Code Online (Sandbox Code Playgroud) 在以下代码中:
#include <iostream>
#include <assert.h>
int main()
{
int a,b;
cin>>a>>b;
char c,d;
cin>>c>>d;
assert ((a==b,a*b==9,c==d));
assert ( a==b && a*b==9 && c==d );
}
Run Code Online (Sandbox Code Playgroud)
如果你 :
第一个断言语句是弱的还是我对断言语句不了解?
有没有办法验证和检查集合数组是否为空。我已经尝试过:
/**
* @Assert\NotBlank()
* @Assert\Length( min = 1)
*/
protected $workPlaces;
public function __construct()
{
$this->workPlaces = new ArrayCollection();
}
Run Code Online (Sandbox Code Playgroud) assert ×10
junit ×3
unit-testing ×3
c++ ×2
testing ×2
android ×1
c# ×1
delphi ×1
delphi-2010 ×1
doctrine-orm ×1
e2e-testing ×1
go ×1
java ×1
javascript ×1
libstdc++ ×1
mocking ×1
moq ×1
robolectric ×1
std ×1
symfony ×1
testcafe ×1