派克斯Moles - 依赖注入

发布于 2024-12-19 18:03:13 字数 671 浏览 2 评论 0原文

我是 Pex 和 Moles 的新手。我正在尝试在我的代码上运行 Pex,但我正在使用构造函数注入。有没有办法指导 Pex 如何注入构造函数?

编辑

    public UserLogic(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public int GetUsersAge(int id)
    {
        User user = _userRepository.GetById(id);
        DateTime DOB = user.DOB;
        DateTime now = DateTime.Today;
        int age = now.Year - DOB.Year;
        if (DOB > now.AddYears(-age)) age--;
        return age;
    }

我需要注入一个存根 userRepository。调用 _userRepository.GetById(id) 时,Pex 失败并出现 NullReferenceException。我一直在使用 Moq 进行单元测试,但我想切换到 pex 和 moles

我应该使用 PexFactories 来创建我的存根吗?

I'm new to Pex and Moles. I am trying to run Pex on my code but I am using Constructor injection. Is there a way of instructing Pex on how to inject the constructors?

Edit

    public UserLogic(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public int GetUsersAge(int id)
    {
        User user = _userRepository.GetById(id);
        DateTime DOB = user.DOB;
        DateTime now = DateTime.Today;
        int age = now.Year - DOB.Year;
        if (DOB > now.AddYears(-age)) age--;
        return age;
    }

I need to inject a stub userRepository. Pex is failing with a NullReferenceException when _userRepository.GetById(id) is called. I have been using Moq for my unit tests but i want to switch to pex and moles

Should i use PexFactories to create my stubs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空气里的味道 2024-12-26 18:03:13

尝试将 Moles Stub 类型传递给 Pex 生成的参数化测试。要创建参数化测试,请右键单击要探索的类,然后选择PEX >创建参数化单元测试。这会在测试类中生成一个包含参数的方法。各个测试调用此参数化测试,发送各个测试参数。

当被测代码通过参数使用依赖注入时(它有一个接口类型参数),Pex 生成的参数化测试方法也将包含相同的接口类型参数。您可以编写自己的测试方法,这些方法也可以调用参数化方法,并为其提供您自己的值,包括接口类型。请确保不要将它们写入 Pex 生成的文件中!

我还建议考虑使用 Mole Stub 类型来进行单元测试注入。 Microsoft Moles 参考手册是一个非常好的开始学习的地方关于如何在单元测试中使用 Moles。当您创建参数化测试时,将为您的接口创建一个摩尔存根类型。只需为您的存根类型配置绕路,然后将其传递给参数化测试即可。

创建存根类型绕道非常容易。我建议在测试项目中创建一个配置方法,配置常用的弯路。我通常添加一个枚举标志作为此方法的参数,这样我就可以轻松地告诉它在某些类型上创建哪些弯路,所有这些都在一次调用中完成。

示例枚举:

[Flags()]
public enum MoleConfigurations
{
    MoleSqlClientObjects,
    DisableDirectory_Exists,
    DisableEventLogExtensions,
    DisableInitializeDatabaseObjects,
    DisableInitializeThreadingObjects,
    DisableQueueExistingDataFiles,
    DisableConstructor
}

Try passing a Moles Stub type to a Pex-generated parameterized test. To create a parameterized test, right-click the class you want to explore, and then select PEX > Create Parameterized Unit Test. This generates a method in the test class that contains arguments. The individual tests call this parameterized test, sending the individual test arguments.

When the code under test uses dependency injection by way of arguments (it has an interface typed argument), the Pex-generated paramterized test method will also contain the same interface type argument. You are able to write your own test methods that also call the parameterized method, feeding it your own values, including the interface type. Just be sure to not write them in the Pex-generated file!

I also suggest looking at using Mole Stub types, for your unit test injections. The Microsoft Moles Reference Manual is a very good place to start learning about how to use Moles in unit tests. A moles stub type will be created for your interface, when you create the parameterized test. Simply configure the detours for your stub type, and then pass it to the parameterized test.

Creating stub type detours is very easy. I suggest creating a configuration method in the test project, that configures frequently-used detours. I usually add an enumeration flag as an argument to this method, so I can easily tell it which detours to create on certain types, all in one call.

Sample enum:

[Flags()]
public enum MoleConfigurations
{
    MoleSqlClientObjects,
    DisableDirectory_Exists,
    DisableEventLogExtensions,
    DisableInitializeDatabaseObjects,
    DisableInitializeThreadingObjects,
    DisableQueueExistingDataFiles,
    DisableConstructor
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文