我正在尝试使用 NUnit
为我的User
类创建一个集成测试,我想做的是启动一个transaction
,更新我的MySQL
测试数据库,然后回滚。
这是我的代码,更新很好,但是我的数据库保持更新。在MySQL Workbench
中调用我的查询工作正常。
[TestFixture]
public class UtenteIntTest
{
private User user;
bool result;
[SetUp]
public void Setup()
{
//Execute query "START TRANSACTION"
result = false;
}
[Test]
public void ConstId_IdUser_User()
{
user = new User(1);
if ((user.id == 1) && user.username == "test" &&
user.name == "test" && user.active == 1
&& user.mail == "test@test.test")
{
result = true;
}
Assert.That(result, Is.EqualTo(true));
}
[Test]
public void Update_User_UpdatedUser()
{
user = new User(1)
{
username = "update",
password = "update",
name = "update",
mail = "update@update.update",
attivo = 0
};
user.Update();
user = new User(1);
if ((user.id == 1) && user.username == "update" &&
user.nominativo == "update" && user.active == 0
&& user.mail == "update@update.update")
{
result = true;
}
Assert.That(result, Is.EqualTo(true));
}
[TearDown]
public void Teardown()
{
//Execute query "ROLLBACK"
}
}
-
检查数据库是否支持事务。
-
您可以使用事务范围来测试事务。请参阅。如何使用 NUnit 测试与数据库相关的代码?