C# 中可读性好的测试名称

发布于 2024-09-16 01:42:18 字数 420 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街女流氓 2024-09-23 01:42:18

不可以,C# 方法名称中不能包含空格或标点符号。你可以这样做:

[TestMethod]
public void When_Asked_Whether_It_Is_On_It_Answers_True() {}

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() {}
躲猫猫 2024-09-23 01:42:18

我首选的测试命名方法根本不包含下划线。

例如,

[Test]
public void SomeMethodShouldCallServiceManagerWithTheCorrectParameters()

话虽如此,一致性是关键。只要您想要方法名称,就不会受到任何惩罚。因此,如果您喜欢用下划线分隔单词,那就这么做吧。

伊恩

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

伤痕我心 2024-09-23 01:42:18

正如书上所说:

public void MethodNameUnderTest_CaseExplained_ExpectedResult()

非常可读

do as the book say:

public void MethodNameUnderTest_CaseExplained_ExpectedResult()

very readable

走走停停 2024-09-23 01:42:18

实际上,如果您确实希望测试中包含空格,则可以执行可怕的 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文