答案 0 :(得分:1)
[TestMethod]
public void CreateCustomer_Successfully()
{
// IsolationLevel.RepeatableRead has to be same as the IsolationLevel in the CustomerService.Handle method.
using (var transaction = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead }))
{
using (var context = ContextHelper.DatabaseContext())
{
// Get number of rows in database.
customerRowsBeforeCreation = context.Customer.Count();
// Arrange: Save Original Customer into database.
customerRepository.Save(customer);
// Arrange CopyCustomer command.
var copyCustomerCommand = new CopyCustomer(){...};
// Act
customerService.Handle(copyCustomerCommand);
// Now i need to get number of customer rows in the database.
// But the last customer insert was not commited, because it is a test.
// So i probably need to call uncommited read, but anyway this row does not end,
// it is waiting infinitely.
// Get number of rows in database.
headerRowsAfterCopy = context.Header.Count();
}
// No scope.Complete => Rollback
// scope.Complete();
}
// Assert
Assert.Equals(0, exceptions.Count());
Assert.Equals(headerRowsBeforeCopy, headerRowsAfterCopy - 1);
}
(及其变体)可用于返回任何数据返回语句的结果,包括Connection.fetch()
。