我正在尝试使用 minitest 和 webrat 测试 ruby 身份验证应用程序,但出现错误。
测试visit '/'
失败并出现错误Status 200 expected but was 404.
包含类似代码的测试fill_in :email, :with => "first@company.com"
失败并出现错误Could not find field: :email
。
我阅读了几个 sinatra、testing 和 webrat 文档和论坛。其中一些是旧的,建议使用 Sinatra::Default 之类的东西,但是github.com/brynary/webrat/wiki/sinatra,构建由 Webrat 测试驱动的 Sinatra 应用程序和向大师学习:Sinatra 内部是新的,但它们仍然失败.
基本上,我不喜欢 rspec、cucumber 等类似句子的语法,但我确实想做行为驱动的开发。我真的很喜欢 minitest 语法,包括测试和输出,这就是我选择 webrat 做 BDD 的原因。如果我对期望 webrat 满足验收测试要求有误,请简单地告诉我应该使用这个框架还是那个框架。
除此之外,主文件和测试文件的第一部分如下。我希望有人能解释我,我错过了什么?
测试文件
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
主文件
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