I assume if you have some typing errors in your code, because your interface is called IAccountService
while your service implements IService
. Your class is called AccountObj
while your constructor is called AccountInfo
.
So let's assume your service is expected to implement IAccountService
and your class is expected to be named AccountInfo
.
When writing a unit test to test AccountInfo.RefreshAmount
, you'll have to be aware of the requirements of this function; you'll have to know exactly what this function is supposed to do.
Looking at the code it seems that the requirements of RefreshAmount
is:
Whatever object that implements IAccountService
and whatever Id are used to construct an object of class AccountInfo
, the post condition of calling RefreshAmount
is that property Amount
returns the same value as IAccountService.GetAccountAmount(Id)
would have returned.
Or more formal:
- For any Id
- For any class that Implements
IAccountService
- The object that is created using Id and IAccountService should after calling RefreshAmount(), return a value for property Amount, equal to the value returned by IAccountService.GetAccountAmount(Id).
Because the requirement says it should work for all Ids and all AccountServices, you can't test your class with all of them. In such cases when writing unit tests you'll have to think of errors that are likely to happen.
In your example it looks that there are no likely errors, but for future versions you could think of that the function would call the service with the incorrect Id, or call the incorrect service, or forgets to save the return value in property Amount, or that property Amount does not return the proper value.
The requirements for your class specify that it should work for every Id and every IAcocuntService. Therefor you are free to provide any Id and AccountService to the constructor of your test object you want to test, that are likely to detect any of the four future errors.
Luckily a Simple AccountInfo class would Suffice
class AccountService : IAccountService
{
public double GetAccountAmount(int accountId)
{
return 137 * accountId - 472;
}
}
The following unit test tests that the Amount is the amount returned by the provided implementation of IAccountService for several Ids
void TestAccountObj_RefreshAmount()
{
const int Id = 438;
IAccountService accountService = new AccountService();
var testObject = new AccountInfo(Id, accountService);
testObject.RefreshAmount();
double amount = testObject.Amount;
double expectedAmount = accountService.GetAccountAmount(Id);
Assert.AreEqual(expectedAmount, amount);
}
This test will test all four mentioned likely errors. The only error that it wouldn't find is that it would call the incorrect service if this incorrect service would return exactly the same strange calculated number. That's why I put such a strange calculation in the service, it is very unlikely that any incorrectly called service would return the same error. Especially if you'd test using various TestObject with various Ids