Comparing byte [] in LINQ-to-SQL and in unit test, which uses mockery

I have the following method:

User IDataContext.AuthenticateUser(string userName, string password) { byte[] hash = PasswordHasher.HashPassword(userName, password); var query = from e in mContext.GetTable<User>() where e.Email == userName && e.Password == hash select e; return query.FirstOrDefault(); } 

When mContext is System.Data.Linq.DataContext , everything works fine. However, when mContext is a layout in memory during my e.Password testing, the comparison between e.Password and hash always returns false .

If I rewrite this comparison as e.Password.SequenceEqual(hash) then my unit tests will pass, but I get an exception when I speak with LinqToSql. (System.NotSupportedException: The query statement "SequenceEqual" is not supported.)

Is there a way that I can write this query that will satisfy my unit tests with a layout in memory, as well as the production component of LinqToSql?

+6
c # unit-testing linq-to-sql mocking
source share
2 answers

This is interesting. I can’t come up with a convenient way to intercept peers without breaking everything, but: unique usernames? You can simply include the username condition in LINQ, and then check the hash in normal C #. As for the transmitted data, there is a slight difference between its transmission and retrieval, especially if we optimize for success (in this case, this reduces the requirements for IO).

Note. If you do, return the same result for "not found" and "not match".

Since byte comparison [] is no longer dependent on special handling using LINQ-to-SQL, it should now work fine in your layout.

 var user = (by name).SingleOrDefault(); if(user==null) #fail bool hashMatch = /* compare array */ if (!hashMatch) #fail return user; 
+3
source share

This is not a good candidate for unit testing. What value is created by testing SQL equality using .Net equality?

In psuedocode, I see (essentially) three method calls:

 HashPassword(); ConstructQuery(); FirstOrDefault(); 

Each of these three named methods can be tested per unit. What is the point of unit testing the original method?

0
source share

All Articles