Reputation: 8418
i have some codes here that retrieve data via linq to sql from a wcf which are abstracted into a dll. whew.
so basically my test involve creating some entries into two table and i need to ensure that table B entry has the correct link to table A entry.
so i was googling around and i found this previous discussion, and the method that look pretty awesome to me was the one using transaction scope.
so i modify my unit test into:
[Test, DataRollBack]
public void TestCreateWarrantNumber_CreateNewWarrant_Return_NewWarrantID()
{
string userName = "headauth";
using (TransactionScope scope = new TransactionScope())
{
IdbRepository repository = new dbRepository();
DataServiceContext db = new DataServiceContext();
int Id = repository.CreateProduct(Ids, userName);
var foundId = (from w in db.Transactions
where w.Ref == Id.ToString()
select w).OrderBy(w => w.Id).FirstOrDefault();
int foundId = 0;
if (foundNumberId != null)
{
foundId = foundNumberId.Id;
}
Assert.AreEqual(Id, foundId,
"Transaction must be assigned successfully to the new product.");
}
}
somehow each time i run this test in vs2010, it doesn't work and i can see in my database this entry is created on the product table each time this test is run.
So i'm thinking the easiest way to test is just to link it to the database and test it that way but what i need now is how do i roll back each time the test is done?
any ideas on what i'm doing wrong here? thanks
After enabling the DTC, i still can't test without doing the data being inserted into the table. and i try nunitx method but also to no avail :(
Upvotes: 0
Views: 407
Reputation: 39898
A Transaction is not by default expanded trough process boundaries. You need to enable this on in your service and then use the correct bindings.
Here you can find some info.
If your WCF service doesn't support transactions and you can't alter the code I think you're stuck with your second option (directly to the database without WCF). Then you're TransactionScope will include your database changes and will automatically rollback when you exit scope (since you're not calling 'Commit')
Upvotes: 1