Linq to sql Moq和数据上下文

Linq to sql Moq和数据上下文,linq-to-sql,moq,Linq To Sql,Moq,我是Moq新手,需要知道我是否做得对 在AccountController.cs中,我有以下内容: int id = _userRepository.GetProfileFromUserName(userName).ProfileID; UserRepository是模拟的,但ProfileID来自DataContext,所以我在我的 AccountControllerTests.cs: mockUserReposository.Setup(gp => gp.Ge

我是Moq新手,需要知道我是否做得对

在AccountController.cs中,我有以下内容:

      int id = _userRepository.GetProfileFromUserName(userName).ProfileID;
UserRepository是模拟的,但ProfileID来自DataContext,所以我在我的 AccountControllerTests.cs:

      mockUserReposository.Setup(gp => gp.GetProfileFromUserName(userName)).Returns(new Profile { ProfileID = 1 });
通过这种方式,我将id变量设置为等于1,并确保在AccountController.cs中调用ProfileID时不使用DataContext中的变量


这样做对吗?或者我是否需要模拟从Linq到SQL的整个概要文件表?

取决于您测试的内容。如果您想要伪造GetProfileFromUserName以粘贴所需的配置文件,那么它看起来很好

你能在你的测试中编辑更多的内容以便人们能给出更完整的见解吗?给出你所说的内容的唯一答案是:D