BDD / TDD with JSpec - 消除代码重复

发布于 2024-09-06 10:51:31 字数 1713 浏览 11 评论 0原文

如何重构以删除本规范中的重复代码:

describe 'TestPlugins'
    describe '.MovieScanner(document)'
        before_each
            MoviePage_loggedIn = fixture("movie_logged_in.html")                // Get logged-in movie page
            MoviePage_notloggedIn = fixture("movie_not_logged_in.html")     // Get non logged-in movie page
            scanner = new MovieScanner()                                                // Get movie scanner
        end

        it 'should scan logged-in movie page for movie data'
            doc = MoviePage_loggedIn            // Get document to scan

            // Unit Tests
            // ------------------------------------------------------------

            // Test movie scanner's functions
            scanner.getMovieTitle(doc).should.eql "The Jacket"
            scanner.getMovieYear(doc).should.eql "2005"

            // Test movie scanner's main scan function
            scannedData = scanner.scan(doc)
            scannedData.title.should.eql "The Jacket"
            scannedData.year.should.eql "2005"
        end

        it 'should scan non logged-in movie page for movie data'
            doc = MoviePage_notloggedIn     // Get document to scan

            // Unit Tests
            // ------------------------------------------------------------

            // Test movie scanner's functions
            scanner.getMovieTitle(doc).should.eql "The Jacket"
            scanner.getMovieYear(doc).should.eql "2005"

            // Test movie scanner's main scan function
            scannedData = scanner.scan(doc)
            scannedData.title.should.eql "The Jacket"
            scannedData.year.should.eql "2005"
        end
    end
end

How do I refactor to remove the code duplication in this spec:

describe 'TestPlugins'
    describe '.MovieScanner(document)'
        before_each
            MoviePage_loggedIn = fixture("movie_logged_in.html")                // Get logged-in movie page
            MoviePage_notloggedIn = fixture("movie_not_logged_in.html")     // Get non logged-in movie page
            scanner = new MovieScanner()                                                // Get movie scanner
        end

        it 'should scan logged-in movie page for movie data'
            doc = MoviePage_loggedIn            // Get document to scan

            // Unit Tests
            // ------------------------------------------------------------

            // Test movie scanner's functions
            scanner.getMovieTitle(doc).should.eql "The Jacket"
            scanner.getMovieYear(doc).should.eql "2005"

            // Test movie scanner's main scan function
            scannedData = scanner.scan(doc)
            scannedData.title.should.eql "The Jacket"
            scannedData.year.should.eql "2005"
        end

        it 'should scan non logged-in movie page for movie data'
            doc = MoviePage_notloggedIn     // Get document to scan

            // Unit Tests
            // ------------------------------------------------------------

            // Test movie scanner's functions
            scanner.getMovieTitle(doc).should.eql "The Jacket"
            scanner.getMovieYear(doc).should.eql "2005"

            // Test movie scanner's main scan function
            scannedData = scanner.scan(doc)
            scannedData.title.should.eql "The Jacket"
            scannedData.year.should.eql "2005"
        end
    end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

樱花坊 2024-09-13 10:51:31

在 BDD 中,我们想要描述我们的应用程序或类的行为,以使它们易于更改。如果删除重复项也会掩盖行为,则不要删除重复项。代码的读取次数比编写次数多 10 倍,对于 BDD 场景和单元级示例,IME 的读取次数甚至更多。

如果您确实决定删除重复项,请将其替换为可读的内容。我不熟悉 JSpec,但我希望能够

scannedData.shouldMatch "The Jacket", "2005"

检查标题和年份的所有相关结果。

要删除与您是否登录无关的重复内容:

将代码分为 Gives(无论您如何到达的上下文)、Whens(通过您实际想要测试其行为的应用程序发生的事件)和 Thens(结果)你正在寻找)。您希望描述系统的功能以及用户可以用它做的事情,而不是它是网页还是窗口 - 这应该不重要。将较低级别的调用放在较低级别。

然后,您可以有两个不同的给定条件 - 已登录或未登录 - 并重复使用其他步骤来完成其余操作。

In BDD, we want to describe the behaviour of our app or classes to make them easy to change. If removing duplication would also obscure the behaviour, don't remove the duplication. The code gets read 10x more than it's written, and IME even more for BDD scenarios and unit-level examples.

If you do decide to remove the duplication anyway, replace it with something readable. I'm not familiar with JSpec but I'd expect something like

scannedData.shouldMatch "The Jacket", "2005"

where all the relevant outcomes for title and year are checked.

To remove the duplication irrelevant of whether you logged in or not:

Separate the code into Givens (context where it doesn't matter how you got there), Whens (events through the app whose behaviour you actually want to test) and Thens (outcomes you're looking for). You're looking to describe the capabilities of the system and things a user can do with it, rather than whether it's a web-page or a window - it shouldn't matter. Put the lower-level calls at a lower level.

You can then have two different givens - logged in or not logged in - and reuse the other steps for the rest.

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