简体   繁体   English

使用rspec失败的测试(将程序映射出来)会导致错误?

[英]Using rspec failed tests (to map the program out) is resulting in errors?

Note: I'm following a tutorial for Ruby on Rails, and thus I'm a complete noob 注意:我正在关注Ruby on Rails的教程,因此我是一个完整的菜鸟

Basically, in the tutorial I am following, it shows you how to make failed tests that you meet as you go along, like a checklist for your program. 基本上,在我所遵循的教程中,它向您展示了如何进行失败的测试,就像程序清单一样。 My problem is that when I do the test, it sort of outputs what it should, but in errors, which isn't good (I think) because it doesn't match the output of the instructions. 我的问题是,当我执行测试时,它输出应有输出,但是会出错,这不好(我认为),因为它与指令的输出不匹配。

Output: http://pastebin.com/0xyf3aBf (and show below) 输出: http : //pastebin.com/0xyf3aBf (如下所示)

Output 产量

Failures:

  1) Static pages Home page should have the content 'Sample App'
     ←[31mFailure/Error:←[0m ←[31mexpect(page).to have_content('Sample App')←[0m

       ←[31mexpected #has_content?("Sample App") to return true, got false←[0m
←[36m     # ./spec/requests/static_pages_spec.rb:9:in `block (3 levels) in <top
(required)>'←[0m

Finished in 0.07 seconds
←[31m1 example, 1 failure←[0m

Failed examples:

←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home p
age should have the content 'Sample App'←[0m

Randomized with seed 10853   

My static_pages_specs.rb file: http://pastebin.com/L0LHACR4 (and shown below) 我的static_pages_specs.rb文件: http : //pastebin.com/L0LHACR4 (如下所示)

static_pages_specs.rb static_pages_specs.rb

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do

    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end
end

Other than the extra control codes (which might just be a symptom from your console or the cut&paste), this looks like a normal test failure. 除了多余的控制代码(可能只是控制台或剪切和粘贴的症状)外,这看起来像是正常的测试失败。 Which you should expect if you have written the test first. 如果您首先编写了测试,应该期待哪一个。

One important part of TDD is indeed to get a simple failing test - with this kind of failure (the have_content matcher has failed to match) - and to then "fix" the failure. TDD的一个重要部分确实是获得一个简单的失败测试-这种失败( have_content匹配器没有匹配)-然后“修复”失败。

It is also possible to get problems with the test code, which would also need fixing of course. 测试代码也可能会出现问题,当然这也需要修复。 But I don't see anything wrong with your test in this case. 但在这种情况下,我认为您的测试没有任何问题。

If there was something wrong with the test, you would have to debug it in the more traditional way of applying critical thinking and experience when you read the code. 如果测试有问题,则在阅读代码时,您将不得不以应用批判性思维和经验的更为传统的方式对其进行调试。 So try to keep the test code simple, and to the point. 因此,请尝试保持测试代码简单明了。 Break down long complex tests into many short small ones. 将复杂的长期测试分解为许多简短的小型测试。

Possibly the have_content matcher could be improved to give better context, rather than make a sub-test that you were not aware of . 可能可以改进have_content匹配器以提供更好的上下文,而不是进行您不知道的子测试。 . . that was written by someone else though, and isn't too bad, it still makes some sense (there is presumably no content at all on the target page). 虽然它是由其他人编写的,但还算不错,但还是有一定道理的(目标页面上根本没有内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM