测试我的MVC存储库的最佳方法是什么?

时间:2021-12-08 20:16:13

I've built a repository and I want to run a bunch of tests on it to see what the functions return.

我已经构建了一个存储库,我想在其上运行一系列测试,以查看函数返回的内容。

I'm using Visual Studio 2008 and I was wondering if there's any sandbox I can play around in (Whether in visual studio 2008 or not) or if I actually have to build a mock controller and view to test the repository?

我正在使用Visual Studio 2008,我想知道是否有任何沙盒我可以玩(无论是否在visual studio 2008中)或者我是否真的需要构建一个模拟控制器并查看测试存储库?

Thanks,
Matt

1 个解决方案

#1


By repository do you mean to say something that is part of your data access layer? If so then what I do is to hook up a clean database as part of my build process (using Nant). This way when I run my build, my clean db is hooked up, any update scripts i have are ran against it to bring it up to speed, then all my unit tests are ran against my code, then my repository tests are ran to insure that my DAL is working as expected, then my db is rebuilt (essentially reset to normal), and then I am ready to go. This way I can pump in and out as much data as I like through my repository to make sure that all of the functions work there...without impacting my day to day development db/data.

通过存储库,您的意思是说一些属于您的数据访问层的内容吗?如果是这样,那么我所做的就是在构建过程中使用干净的数据库(使用Nant)。这样当我运行我的构建时,我的干净数据库被连接起来,我运行的任何更新脚本都会对它进行运行以提高速度,然后我的所有单元测试都针对我的代码运行,然后我的存储库测试运行以确保我的DAL按预期工作,然后重建我的数据库(基本上重置为正常),然后我准备好了。这样我可以通过我的存储库输入和输出尽可能多的数据,以确保所有功能在那里工作......而不会影响我的日常开发数据库/数据。

If you just run tests on the your working db then you run into the problem that the data may change which might break your tests. If as part of your tests you pump known data in, and then run tests on your repository, the outcome is assumed to be known and should not change over time. This makes your test more likely to endure through time.

如果您只是在您的工作数据库上运行测试,那么您会遇到数据可能会更改的问题,这可能会破坏您的测试。如果作为测试的一部分,您将已知数据泵入,然后在您的存储库上运行测试,则假定结果是已知的,并且不应随时间变化。这使您的测试更有可能随着时间的推移而持久。

Hope this is what you meant!

希望这就是你的意思!

#1


By repository do you mean to say something that is part of your data access layer? If so then what I do is to hook up a clean database as part of my build process (using Nant). This way when I run my build, my clean db is hooked up, any update scripts i have are ran against it to bring it up to speed, then all my unit tests are ran against my code, then my repository tests are ran to insure that my DAL is working as expected, then my db is rebuilt (essentially reset to normal), and then I am ready to go. This way I can pump in and out as much data as I like through my repository to make sure that all of the functions work there...without impacting my day to day development db/data.

通过存储库,您的意思是说一些属于您的数据访问层的内容吗?如果是这样,那么我所做的就是在构建过程中使用干净的数据库(使用Nant)。这样当我运行我的构建时,我的干净数据库被连接起来,我运行的任何更新脚本都会对它进行运行以提高速度,然后我的所有单元测试都针对我的代码运行,然后我的存储库测试运行以确保我的DAL按预期工作,然后重建我的数据库(基本上重置为正常),然后我准备好了。这样我可以通过我的存储库输入和输出尽可能多的数据,以确保所有功能在那里工作......而不会影响我的日常开发数据库/数据。

If you just run tests on the your working db then you run into the problem that the data may change which might break your tests. If as part of your tests you pump known data in, and then run tests on your repository, the outcome is assumed to be known and should not change over time. This makes your test more likely to endure through time.

如果您只是在您的工作数据库上运行测试,那么您会遇到数据可能会更改的问题,这可能会破坏您的测试。如果作为测试的一部分,您将已知数据泵入,然后在您的存储库上运行测试,则假定结果是已知的,并且不应随时间变化。这使您的测试更有可能随着时间的推移而持久。

Hope this is what you meant!

希望这就是你的意思!