This project is read-only.

Givens do not output to resharper

Jul 13, 2009 at 9:58 PM

I have the following code in a base class to get NBehave to output to resharper when running tests:

Story.MessageAdded += (sender, e) => Console.WriteLine(e.EventData.Message);
            Story.ScenarioCreated += (sender, e) => Console.WriteLine(e.EventData.Title);
            Story.StoryCreated += (sender,e) => Console.WriteLine(e.EventData.Title);


Unfortunately, this does not output the "givens" in my specs.  Looking at the source code, I see a lot of calls to "AddBlankMessage()" like below.  Is this by design?  How can i get my given conditions to output?

        public GivenFragment Given(string context, Action action)
            if (CanAddMessage)

                _story.InvokeAction(GivenType, context, action);

            return new GivenFragment(this);


Jul 17, 2009 at 11:48 PM

I cant test with ReSharper, I have no licence and the trial period has run out :(
NBehave uses the same events as you use to produce its output, for example the xml output and there the givens are included.
I did try out to put the line Story.MessageAdded += (sender, e) => Console.WriteLine(e.EventData.Message);
into NBehave.Console.Program's Main method which is the starting point for the console runner. I did get Givens printed to my console. I know its not the same thing as running it with Resharper but I think we can rule out any bugs with Givens not being sent to the MessageAdded event.
I would really like to see your base class, if its possible and an example of how to use it.

Jul 21, 2009 at 12:26 AM

I don't think this is a resharper issue.  It happens when I just run the tests thru the console, and as I said the source code appears to not be doing anything with the message.

Jul 23, 2009 at 11:43 AM

Which version of NBehave are you running? The code snippet above points to code before the 0.4 release. I think the best thing would be to grab the trunk and see what happens. Installer for latest build is here: if you dont want to build it yourself.

Jul 23, 2009 at 6:56 PM

Thanks I'll try that.  It was also showing the "And"s from the given but not the given itself.  Probably just an old version.

Jul 23, 2009 at 7:30 PM

That fixed it.  Just an old version i inherited from another developer.  I can even see it in resharper.  Thanks!

Jul 27, 2009 at 7:36 PM

Well i was wrong.

Its still not working.  I even pulled down trunk from google code and built it and it doesnt work.  It was working on certain tests.  Basically if i use a Given with only the one string parameter like this:

.Given("something ahead of time...")

I get no console output, not in resharper or plain old command line.  However if I add an action parameter like so:

.Given("something else", () => {} )

Then the output shows up.  I think its just a different code path.  Any ideas???


Jul 28, 2009 at 2:12 AM

Here's the class I wrote to get everything output with a nice format like the examples have:

public class SpecBaseWithConsoleOutput : SpecBase


private EventHandler<EventArgs<MessageEventData>> _addedHandler;

private EventHandler<EventArgs<Scenario>> _scenarioCreatedHandler;

private EventHandler<EventArgs<Story>> _storyCreatedHandler;

private const string ScenarioType = "Scenario";

private const string NarrativeTabs = "\t\t";

private const string ScenarioTabs = "\t\tScenario ";

private const string MessageTabs = "\t\t\t\t";

private int _scenarioNumber = 1;

private string _lastType = "";

public override void MainSetup()



_storyCreatedHandler = (o, a) => Console.WriteLine("\nStory: " + a.EventData.Title + "\n\nNarrative:");

_scenarioCreatedHandler = (o, a) =>


Console.WriteLine("\n" + ScenarioTabs + _scenarioNumber++ + ": " +


_lastType = ScenarioType;


_addedHandler = (o, a) =>


switch (a.EventData.Type)


case "Narrative":

Console.WriteLine(NarrativeTabs + a.EventData.Message);


case "Given":

if (_lastType != ScenarioType)


Console.WriteLine(MessageTabs + a.EventData.Message);



Console.WriteLine(MessageTabs + a.EventData.Message);



_lastType = a.EventData.Type;



Story.StoryCreated += _storyCreatedHandler;

Story.ScenarioCreated += _scenarioCreatedHandler;

Story.MessageAdded += _addedHandler;


public override void MainTeardown()


Story.MessageAdded -= _addedHandler;

Story.ScenarioCreated -= _scenarioCreatedHandler;

Story.StoryCreated -= _storyCreatedHandler;





Jul 28, 2009 at 11:32 AM

A fix for not writing the Given to the output is now committed.
Please note that to use a Given with only a string parameter you must first have registered a Given with an action so that the "string only" Given can reuse the registered action.

Given("the account balance is $balance", 20, accountBalance => { })
can be reused with
Given("the account balance is 50")

but Given("something") will now fail properly.