使用Webrat的sinatra应用程序的验收测试失败 [英] Acceptance testing of sinatra app using webrat fails

查看:117
本文介绍了使用Webrat的sinatra应用程序的验收测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用minitest和webrat测试ruby身份验证应用程序,但会收到错误消息.

I am trying to test a ruby authentication app using minitest and webrat but get errors.

诸如visit '/'的测试失败,并显示错误Status 200 expected but was 404.

Tests like visit '/' fail with an error Status 200 expected but was 404.

包含诸如fill_in :email, :with => "first@company.com"之类的代码的测试失败,并显示错误Could not find field: :email.

Tests containing code like fill_in :email, :with => "first@company.com" fail with error Could not find field: :email.

我阅读了一些sinatra,测试和webrat文档以及论坛.其中一些较旧,建议使用类似Sinatra :: Default之类的东西,但 github.com/brynary/webrat/wiki/sinatra 构建由Webrat测试驱动的Sinatra应用向大师学习:Sinatra Internals 是新手,但仍然失败.

I read several sinatra, testing and webrat documents and forums. Some of them were old and suggested stuff like Sinatra::Default, but github.com/brynary/webrat/wiki/sinatra, Building a Sinatra App Driven By Webrat Tests and Learning From the Masters: Sinatra Internals are new, yet they still fail.

基本上,我不喜欢rspec,cumcum等类似句子的语法,但确实想进行行为驱动的开发.我真的很喜欢最小测试语法,包括测试和输出,这就是为什么我选择webrat作为BDD的原因.如果我对期望Webrat满足验收测试要求有误,请简单告诉我应该使用此框架或该框架.

Basically, I didn't like sentence-like syntax of rspec, cucumber etc but do want to do behaviour driven development. I really like the minitest syntax, both tests and output and that is why I choose webrat for BDD. If I'm wrong about expecting webrat to fulfill acceptance testing requirements, please simply tell me that I should use this framework or that one.

除此之外,主文件和测试文件的第一部分如下.我希望有人能解释我,我想念的是什么?

Apart from that, the first parts of the main file and test file are below. I hope someone can explain me, what I am missing?

require "test/unit"
require "minitest/autorun"
require "rack/test"
require 'webrat'
require_relative "../lib/kimsin.rb"

Webrat.configure do |config|
  config.mode = :rack
end

ENV["RACK_ENV"] = "test"

class KimsinTests < Test::Unit::TestCase
  include Rack::Test::Methods
  include Webrat::Methods
  include Webrat::Matchers

  def app
    Sinatra::Application.new
  end

  def test_create_user
    visit "/user/new"
    fill_in :username, :with => "first@company.com"
    fill_in :password, :with => "abC123?*"
    fill_in :confirm_password, :with => "abC123?*"
    click_link "Register"
    assert 201, last_response.status, "Status 201 expected but was #{last_response.status}.\n#{error}"
    assert_contain /Logged in as first@company.com./, "No user created"
    assert_contain /Logout/, "Logout link not present"
  end

main_file

require "sinatra"
require "erb"
require_relative "../lib/kimsin/version"
require_relative "../lib/kimsin/user"

class Kimsin < Sinatra::Application
  use Rack::Session::Pool, :expire_after => 2592000
  set :session_secret, BCrypt::Engine.generate_salt

  configure :development do  
    DataMapper.auto_migrate!  
  end

  get "/" do
    if session[:user_id]
      user = User.get session[:user_id]
      email = user.email
      erb :index, :locals => { :email => email }
    else
      email = nil
      erb :index, :locals => { :email => email }
    end      
  end

推荐答案

将Sinatra与Webrat一起使用应该可以正常工作.我认为您看到的错误是由以下方法引起的(测试文件中的第18行):

Using Sinatra with Webrat should work fine. I think that the errors that you are seeing are caused by the following method (around line 18 in your test file):

def app
  Sinatra::Application.new
end

这是在您确实需要设置自己的子类Kimsin(因为正在创建模块化样式Sinatra应用程序)时(即

This is setting up the Sinatra::Application base class to run your tests against when you really need to set up your own subclass Kimsin (because you are creating a modular style Sinatra app), i.e.

def app
  Kimsin.new
end

由于Sinatra :: Application没有定义您要测试的任何路由,因此发生了404错误和缺少字段.

The 404 errors and missing fields are happening because Sinatra::Application doesn't define any of the routes you are testing.

如果您正在寻找Webrat的替代产品,您可能还想看看水豚.

You might also like to take a look at Capybara if you are looking for similar alternatives to Webrat.

这篇关于使用Webrat的sinatra应用程序的验收测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆