使用Behaves_like在基类上
我想在基本规范上指定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当前仅上下文类支持行为,基类不支持行为。
将ExecuteMethodOverridableBehavior
行为的It
直接纳入基类中是否可行? (它
位于基类中,将在派生上下文执行时执行。)抱歉,在写上面的答案时我一定是疯了。基类
不支持
It
字段,仅支持Establish
和Because
。虽然只能有一个Because
,但层次结构中可能有多个Establish
子句。我担心将行为 (
Behaves_like
) 放在所有派生类上是唯一的方法。Behaviors are currently only supported on context classes, not base classes.
Would it be feasable to factor theIt
s of theExecuteMethodOverridableBehavior
behavior right into the base class? (It
s 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, onlyEstablish
andBecause
. While there can be only oneBecause
, there might be multipleEstablish
clauses in the hierarchy.I fear putting the behavior (
Behaves_like
) on all derived classes is the only way to go.