This project is read-only.

Plain text scenarios

Sep 24, 2009 at 11:37 PM

I'm keen to start applying NBehave plain text scenarios on my project, but I have a few questions I'm hoping someone can help me with:

  1. How does NBehave match up the [ActionSteps] class against the .scenario file?  I expect I'd want a number of different [ActionSteps] classes.
  2. The example GameSpec.cs class stores some state in the [ActionSteps] class.  Does NBehave create an instance of this class per scenario?
  3. Is there any equivalent to the classic TDD [Setup] and [Teardown] methods?

TIA,

John

Sep 28, 2009 at 11:15 AM

1. If you use the 0.4 release the first action that matches is used, if you go by trunk you can implement the interface IMatchFiles in your [ActionSteps] classes to get control over which scenario files "belongs" to which class(es).

2. NBehave creates the instance once so that instance is reused between scenarios

3. There are no [SetUp] or [Teardown] like attributes, I have considered adding that. Feel free to send me a patch for it :)

 

Sep 28, 2009 at 8:28 PM

The IMatchFile interface was exactly what I was after.  Thanks!

I've opened issue 47 on the Google code site for Setup / TearDown style attributes in plain text scenarios, and I've uploaded a patch.  Please can you review and commit it.

Cheers,
John 

Sep 28, 2009 at 9:56 PM

patch committed.

Mar 26, 2010 at 3:38 PM

Hi John,

Could you please post a bit of code to show how to use the IFileMatcher?

Thank you very much.

Cedric

Apr 6, 2010 at 2:32 AM

Cedric,

Your [ActionSteps] class needs to implement IMatchFiles, which defines a read-only FileMatcher property (code for the interface is viewable http://code.google.com/p/nbehave/source/browse/trunk/src/NBehave.Framework/Text/IMatchFiles.cs?spec=svn173&r=173).  Your implementation of IFileMatcher can then match using whatever algorithm you think best.  Here's one I prepared earlier which assumes that .scenario files are grouped into directories that correspond to namespaces in code (although only a single level of nesting is used):

 

    [ActionSteps]
    public class ProformaSteps : ActionStepsBase, IMatchFiles
    {

 

 

[ActionSteps]
public class ProformaSteps : IMatchFiles
{
        public IFileMatcher FileMatcher
        {
            get { return new MatchDirectoryToNamespace(GetType()); }
        }

        // Snip
}

public class MatchDirectoryToNamespace : IFileMatcher
{
    private readonly string _nameToMatch;

    public MatchDirectoryToNamespace(Type actionStepClass)
    {
        _nameToMatch = actionStepClass.Namespace
                                    .Split('.')
                                    .AsEnumerable()
                                    .Last();
    }

    public bool IsMatch(string fileName)
    {
        var info = new DirectoryInfo(fileName);
        return info.Parent.Name == _nameToMatch;
    }
}

HTH,

John