English 中文(简体)
Moq and DataContext
原标题:

I am new to Moq and need to know if I am doing this right.

In AccountController.cs I have this:

      int id = _userRepository.GetProfileFromUserName(userName).ProfileID;

UserRepository is mocked, but ProfileID comes from DataContext, so I did this in my AccountControllerTests.cs:

      mockUserReposository.Setup(gp => gp.GetProfileFromUserName(userName)).Returns(new Profile { ProfileID = 1 });

This way I get the id variable to be equal to 1, and ensure that ProfileID doesn t use the one from DataContext when called in AccountController.cs

Is this the right way to do it? Or do I somehow need to mock my whole Profile table from Linq to SQL?

问题回答

Depends what you re testing. If you re looking to Fake the GetProfileFromUserName for the purposes of sticking in a desired Profile, it looks fine.

Can you edit in more of your test so people can give more complete insights - the only answer given how much you ve said is It Depends :D





相关问题
LINQ to SQL as databinding source for WPF Treeview

I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...

Linq to SQL insert/select foreign/primary records

I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...

LINQ to SQL Dynamic Sort question

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...

Using a schema other than dbo in LinqToSql Designer

Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...

热门标签