2011-08-14 29 views
5

Tôi đang gặp sự cố khi hiểu cách làm việc với Rack :: Test, vấn đề tôi gặp phải là POST. Đây là lớp học và các lỗi:Cách đăng dữ liệu trong Rack :: Thử nghiệm

hellotesting.rb

require 'sinatra' 

post '/foo' do 
    "Hello #{params[:name]}." 
end 

Đây là thử nghiệm:

require 'hellotesting' 
require 'test/unit' 
require 'rack/test' 

set :environment, :test 

class HelloWorldTest < Test::Unit::TestCase 
    def test_it_says_hello_to_you 
     browser = Rack::Test::Session.new(Rack::MockSession.new(Sinatra::Application)) 
    post "/foo", "name" => "Bryan" 
     assert browser.last_response.ok? 
     assert_equal 'Hello Bryan', browser.last_response.body 
    end 
end 

Và kết quả:

1) Error: 
test_it_says_hello_to_you(HelloWorldTest): 
ArgumentError: wrong number of arguments (1 for 0) 
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `name' 
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `send' 
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `compile!' 
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `each_pair' 
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1141:in `compile!' 
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1129:in `route' 
/Library/Ruby/Gems/1.8/gems/sinatra-1.2.6/lib/sinatra/base.rb:1118:in `post' 
(__DELEGATE__):3:in `send' 
(__DELEGATE__):3:in `post' 
testingjeison.rb:11:in `test_it_says_hello_to_you' 

Trả lời

4

Nó có thể là bạn cần bao gồm Rack :: Test mixins vào các lớp riêng lẻ của bạn. Tôi chủ yếu sử dụng RSpec, mà không sử dụng các lớp học, nhưng không sử dụng một biến thể chuyên biệt của Ruby include để kéo thêm chức năng. Bạn có thể muốn thử đặt trong include Rack::Test::Methods bên trong định nghĩa lớp trường hợp HelloWorldTest của bạn. Số testing của Sinatra có thêm thông tin để thử nghiệm với Rack::Test.

+0

Thật vậy, việc thêm mixin vào thử nghiệm của tôi và sửa đổi một vài biến đã sửa nó. Cảm ơn! – ferostar

Các vấn đề liên quan