views:

129

answers:

4

I read in FsUnit that the following are valid method/classnames in F#:

[<TestFixture>] 
type ``Given a LightBulb that has had its state set to true`` ()=
   let lightBulb = new LightBulb(true)

   [<Test>] member test.
    ``when I ask whether it is On it answers true.`` ()=
       lightBulb.On |> should be True

Is there a way to have method or classnames like this in c#?

+6  A: 

No, you cannot have spaces or punctuation in a C# method name. You can do:

[TestMethod]
public void When_Asked_Whether_It_Is_On_It_Answers_True() {}
driis
This is what I expected. I had to ask anyhow... Having to write all those underscores does not come naturally to me... And having testnames without underscores would improve readability on test-reports.
Nils
Interestingly you can add escape characters to C# identifier names, so cl\u0061ss is a valid name. However, the escape sequence for space is not valid, so "When\u0021Asked" is not a valid identifier, which rather seems to defeat the point of allowing escape sequences in identifiers. (Admittedly this still looks ugly in source but at least would look nice in a test runner app).
Robert
@Robert - Assuming C# is like Java in this regard, the compiler converts the escape characters to the none-escaped form. It is basically to allow an ASCII files to handle none-ASCII identifiers.
mlk
Why not have a test report tool show test names by replacing underscores with spaces?
Alexey Romanov
+1  A: 

do as the book say:

public void MethodNameUnderTest_CaseExplained_ExeptedResult()

very readable

Chen Kinnrot
Which book are you referring to?
John Saunders
The art of unit test
Chen Kinnrot
+1  A: 

My preferred method of naming tests does not include underscores at all.

For example,

[Test]
public void SomeMethodShouldCallServiceManagerWithTheCorrectParameters()

With that said, consistency is key. There is no penalty for having a method name as long as you want it. So if you prefer to separate words with underscores, then go for it.

Ian

Ian P
Having it that way is a pain to read, IMO. It's def harder than reading it with underscores.
devoured elysium
A: 

Actually, there's a horrendous Unicode hack you can do if you really want your tests to have spaces in them.

Jimmy Bogard explains:

http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/02/16/spaces-in-identifier-names-in-c.aspx

Basically, use a UTF-16 0x200E character as space, hacking your keyboard with AutoHotkey so that you can easily type that character when you need to.

Now the question is, do you really want to do that?

Dan Fitch