我正在测试一个提供 UIAlertController 的函数,但测试一直失败。这是函数的样子:
func presentBuyingErrorDialogue() {
let alert = UIAlertController(
title: "Warning",
message: "Error purchasing item, please retry this action. If that doesn't help, try restarting or reinstalling the app.",
preferredStyle: .alert
)
let okButton = UIAlertAction(title: "OK", style: .default)
alert.addAction(okButton)
self.present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
由于此函数位于名为 ShopViewController 的类中,因此我假设测试此函数的正确方法是调用该函数shopViewController.presentBuyingErrorDiaologue()然后使用XCTAssertTrue(shopViewController.presentedViewController is UIAlertController). 但是,当我运行测试时,断言语句失败。测试 UIAlertController 是呈现视图的正确方法是什么?
我是robolectric的新手,我正在尝试对创建AlertDialog的按钮进行测试。单击该按钮后,将使用我想使用Robolectric单击的肯定按钮创建AlertDialog,并测试它是否启动活动。这是按钮的代码:
newUserButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
builder.setTitle(context.getResources().getString(R.string.start_title_message))
.setMessage(getResources().getString(R.string.start_dialog_message));
builder.setPositiveButton(getString(R.string.start_confirm_message), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivityForResult(new Intent(StartActivity.this, AvatarRoomActivity.class), 0);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
ColorDrawable drawable = new ColorDrawable(Color.WHITE);
drawable.setAlpha(200);
dialog.getWindow().setBackgroundDrawable(drawable);
dialog.show();
}
});
Run Code Online (Sandbox Code Playgroud)
有谁知道我可以测试单击肯定按钮,然后启动AvatarRoomActivity吗?在此先感谢您,希望很快能收到您的来信。