首先,你不想要測試的內置JSON的轉換散列。同樣適用於xml。
你測試控制器的數據作爲散列,而不是打擾它的JSON,XML或從一個HTML表單。
但是,如果你想這樣做,作爲一個練習,這是一個獨立的Ruby腳本做把玩:)
require 'json'
url = URI.parse('http://localhost:3030/mymodels.json')
request = Net::HTTP::Post.new(url.path)
request.content_type="application/json"
request.basic_auth('username', 'password') #if used, else comment out
hash = {:mymodel => {:name => "Test Name 1", :description => "some data for testing description"}}
request.body = hash.to_json
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
puts response
切換到XML,使用content_type="text/xml"
和
request.body = "<?xml version='1.0' encoding='UTF-8'?><somedata><name>Test Name 1</name><description>Some data for testing</description></somedata>"
來源
2010-11-10 12:24:58
oma
通常你POST在控制器的動作。你爲什麼張貼到一個文件? – 2010-10-27 23:15:35
這是一個RESTful模型 – skeevis 2010-10-28 01:28:57