ruby-on-rails - 为什么这个 Rails Controller 测试失败了?

标签 ruby-on-rails testing tdd bdd shoulda

我正在尝试了解此测试失败的原因。 (我是测试新手。)我正在使用内置的 Rails 测试框架,并添加了 Shoulda gem。

测试:

require 'shoulda'

context "on GET to :new" do

  setup do
    get(:new)
  end

  should_render_template :new
  should_not_set_the_flash

end

失败:

1) Failure:
test: on GET to :new should render template :new. (SessionsControllerTest)
[/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/controller   /macros.rb:220:in `__bind_1233882600_699194'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `call'
/usr/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.0.6/lib/shoulda/context.rb:254:in `test: on GET to :new should render template :new. '
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/testing/setup_and_teardown.rb:94:in `run']:
expecting <"new"> but rendering with <"">

2 tests, 2 assertions, 1 failures, 0 errors

但是如果我在控制台上使用 app.get '/sessions/new' 运行它,它可以正常工作,没有错误。

new”模板在浏览器中按预期呈现。

我正在使用 Haml。也许这会导致问题。我的模板名为“new.html.haml”。

最佳答案

失败表示没有呈现模板。尝试确保您没有被重定向

should_respond_with :success

并断言 @response.body 的内容只是为了查看发回的内容

# This will fail but should give you some clue about what was sent back.
should "sent something back in the body" do
  assert_match 'boohooo', @response.body
end

你也可以断言一个特定的模板,所以你也可以试一试:

should_render_template "new.html.haml"

但是,我不怀疑 HAML 是导致您出现问题的原因。

关于ruby-on-rails - 为什么这个 Rails Controller 测试失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/518767/

相关文章:

javascript - 如何启用按钮检查单选按钮

ruby-on-rails - Capistrano 迁移部署

ruby-on-rails - 生成内容丰富的博客时,Rails Ruby Gems 与纯开发对比

rest - 如何使用针对多个微服务的REST API测试来对测试框架进行版本控制/标记?

python - 使用数据访问层和应用层时的 TDD 方法

ruby-on-rails - 如何批量创建帖子?

javascript - 如何将 QUnit 测试集成到 Yii 中

angularjs - 我应该在 AngularJS 中测试什么?

java - Java中的重构方法: What pattern should I apply?

php - Laravel 5.1 + PHPunit - API 测试总是返回无效参数错误