开发者

Using Behaves_like<TBehavior> on a Base Class

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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜