使用Behaves_like在基类上

发布于 2024-09-14 14:33:52 字数 1214 浏览 4 评论 0原文

我想在基本规范上指定 Behaves_like 以确保特定方法被标记为虚拟。像这样的事情:

public abstract class command_handler_context<TCommandHandler, TCommand> 
    : abstract_context<TCommandHandler>
    where TCommandHandler : ICommandHandler<TCommand>
    where TCommand : ICommand, new()
{
    protected static TCommand Command;
    private Establish context = () =>
    {
        Command = new TCommand();
    };
    private Because of = () => SubjectUnderTest.Execute(Command);

    private Behaves_like<ExecuteMethodOverridableBehavior<TCommandHandler>> an_overridable_execute_method;
}

但是测试运行者并没有注意到这一点。我认为在命令处理程序的每个规范上指定 ehaves_like 将是一个主要的 PITA。这可能吗?如果不是,这是期望的行为吗?

更新:抱歉回复晚了,这是失败的规范:

public abstract class context_base
{
    protected static bool Result;
    protected static bool RanOnBaseClass;
    private Because of = () => { Result = true; };

    private It should_be_true = () =>
    {
        RanOnBaseClass = true;
        Result.ShouldBeTrue();
    };
}
public class when_using_behaviors_on_a_base_class
    : context_base
{
    private It should_run_specs_on_the_base_class = () => RanOnBaseClass.ShouldBeTrue();
}

I would like to specify a Behaves_like on a base specification to ensure that a particular method is marked as virtual. Something like this:

public abstract class command_handler_context<TCommandHandler, TCommand> 
    : abstract_context<TCommandHandler>
    where TCommandHandler : ICommandHandler<TCommand>
    where TCommand : ICommand, new()
{
    protected static TCommand Command;
    private Establish context = () =>
    {
        Command = new TCommand();
    };
    private Because of = () => SubjectUnderTest.Execute(Command);

    private Behaves_like<ExecuteMethodOverridableBehavior<TCommandHandler>> an_overridable_execute_method;
}

However the test runner does not pick this up. I think it would be a major PITA to specify ehaves_like on every single spec for a command handler. Is this possible? If not, is this a desired behavior?

Update: Sorry about the late response, here is the failing spec:

public abstract class context_base
{
    protected static bool Result;
    protected static bool RanOnBaseClass;
    private Because of = () => { Result = true; };

    private It should_be_true = () =>
    {
        RanOnBaseClass = true;
        Result.ShouldBeTrue();
    };
}
public class when_using_behaviors_on_a_base_class
    : context_base
{
    private It should_run_specs_on_the_base_class = () => RanOnBaseClass.ShouldBeTrue();
}

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

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

发布评论

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

评论(1

不离久伴 2024-09-21 14:33:52

当前仅上下文类支持行为,基类不支持行为。 ExecuteMethodOverridableBehavior 行为的 It 直接纳入基类中是否可行? (位于基类中,将在派生上下文执行时执行。)

抱歉,在写上面的答案时我一定是疯了。基类不支持It字段,仅支持EstablishBecause。虽然只能有一个 Because,但层次结构中可能有多个 Establish 子句。

我担心将行为 (Behaves_like) 放在所有派生类上是唯一的方法。

Behaviors are currently only supported on context classes, not base classes. Would it be feasable to factor the Its of the ExecuteMethodOverridableBehavior behavior right into the base class? (Its in base classes will be executed when derived contexts execute.)

Sorry, I must have been out of my mind when writing the answer above. It fields are not supported on base classes, only Establish and Because. While there can be only one Because, there might be multiple Establish clauses in the hierarchy.

I fear putting the behavior (Behaves_like) on all derived classes is the only way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文