【问题标题】:My function works in practice but does not pass tests rspec我的功能在实践中有效,但未通过测试 rspec
【发布时间】:2020-09-30 07:44:09
【问题描述】:

也许是我对 rspec 不熟悉,但我不明白我的测试发生了什么。

我有 2 个类,一个叫做 Scrape,另一个 Result(创意)Scrape 是一个网络抓取类,它搜索站点并从页面中抓取结果,从每个类中创建一个新的 Result 实例。

结果实例存储在可通过 Result.all 访问的类变量数组中

这在实际程序中实际上是有效的,但是当我尝试为此行为编写测试时它失败了。

    describe "#scrape_results" do
        it "accepts a url scrapes the page and creates a Result for each" do
            s = Scrape.new
            s.scrape_results(@url)
            expect(Result.all.count).not_to eq(0)
        end
    end

每次我运行测试 Result.all.count 都是 0 如果我使用 pry 并手动运行 #scrape_results 测试通过。

感谢您的时间、耐心和帮助 谢谢

【问题讨论】:

  • 你有存根吗?你能在你的scrape_results 方法中设置一个断点并找出为什么没有创建记录吗?

标签: ruby testing rspec tdd ruby-test


【解决方案1】:

我注意到您在测试中将@url 传递给#scrape_results。除非您在 describe 块或 test 块中定义该变量,否则它将在您的测试中为 nil。有可能因为 @url 可能不是 nil 从任何您正在窥探的地方,这导致创建结果并通过测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2013-06-10
    • 1970-01-01
    相关资源
    最近更新 更多