C# 中可读性好的测试名称
我在 FsUnit 中读到,以下是 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
有没有办法让方法或C# 中有这样的类名吗?
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#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不可以,C# 方法名称中不能包含空格或标点符号。你可以这样做:
No, you cannot have spaces or punctuation in a C# method name. You can do:
我首选的测试命名方法根本不包含下划线。
例如,
话虽如此,一致性是关键。只要您想要方法名称,就不会受到任何惩罚。因此,如果您喜欢用下划线分隔单词,那就这么做吧。
伊恩
My preferred method of naming tests does not include underscores at all.
For example,
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
正如书上所说:
非常可读
do as the book say:
very readable
实际上,如果您确实希望测试中包含空格,则可以执行可怕的 Unicode hack。
吉米·博加德解释道:
http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/02/16/spaces-in-identifier-names-in-c.aspx
基本上,使用 UTF-16 0x200E 字符作为空格,使用 AutoHotkey 破解键盘,以便您可以在需要时轻松键入该字符。
现在的问题是,你真的想这样做吗?
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?