Hi Alex,
If you want to test the linq expression you probably want to fake DataContext.GetTable or extract the the data base access to a separate class which seems like a better idea :)
Here's an example:
public class DataAccsess
{
public List<DbAccountCredential> GetTable()
{
var db = new DataContext(DataBaseUtil.ConnectionString);
return db.GetTable<DbAccountCredential>().ToList();
}
}
public long GetUserIdByUsernameAndPassword(string username, string password)
{
DataAccsess dataAccsess = new DataAccsess();
var table = dataAccsess.GetTable();
var query =
from credentials in table
where credentials.UserName == username && credentials.PassWord == password
select (new { credentials.AccountId });
return query.SingleOrDefault().AccountId;
}
[Test]
public void Test()
{
var fakeList = new List<DbAccountCredential>();
var fake = Isolate.Fake.Instance<DataAccsess>();
Isolate.WhenCalled(() => fake.GetTable()).WillReturn(fakeList);
Isolate.Swap.NextInstance<DataAccsess>().With(fake);
var foo = new Foo();
long actual = foo.GetUserIdByUsernameAndPassword("user", "password");
//...
}
Hope that I guessed right about what you want to test :D
Please let me know if it helps.