Mike Clark‘s Weblog:Running Your Rails App Headless
来源:百度文库 编辑:神马文学网 时间:2024/04/28 18:52:39
Better Software Faster
Mike Clark‘s Weblog
Syndicate (RSS) | Archives
(Tue 04 Apr 2006) [/Rails] #
If you‘ve ever wanted to poke and prod your Rails app through the entire stack without firing up a web browser, Rails 1.1 just made it easy. You can now use the app object from within script/console to get access to theActionController::Integration::Session instance. And that means you can drive your app just like an integration test.
Let‘s take an app for a quick test drive:
$ script/consoleLoading development environment.>> app.class=> ActionController::Integration::Session
Going to Fred‘s home page should redirect him to a login page:
>> app.get "/home"=> 302>> app.controller.params=> {"action"=>"home", "controller"=>"accounts"}>> app.response.redirect_url=> "http://www.example.com/login"
Sure enough. Now let‘s log ol‘ Fred in:
>> app.post "/login", :user => {:login => ‘fred‘, :password => ‘flintstone‘}=> 302>> app.session[:user_id]=> 1>> app.cookies=> {"shopper_id"=>"176f8c0bf450f772f80f85ba22861504"}
Since Fred originally intended to hit his Home page, now that he‘s logged in he should have landed up back on his Home page with a cheery greeting:
>> app.response.redirect_url=> "http://www.example.com/home">> app.flash=> {:notice=>"Welcome Back, fred!"}
Being an avid shopper, Fred can‘t help but buy something. In this case, we use an asynchronous (yes, that‘s Ajaxy) request:
>> app.xml_http_request "/store/add_to_cart", :id => 1=> 200>> app.assigns(:new_item)=> #This isn‘t a regression testing tool, mind you. But it‘s quite handy as an experiential interface to your app. And when you‘re satisfied with the results, you can (and should!) write an automatedintegration test. The app instance used above is the same object that‘s used during integration testing, but it‘s implicit in the integration test. So you could turn around and convert the session above into something like the following:
require "#{File.dirname(__FILE__)}/../test_helper"class ShoppingTest < ActionController::IntegrationTestfixtures :productsdef test_shoppingget "/home"is_redirected_to "login"post "/login",:user => {:login => ‘fred‘, :password => ‘flintstone‘}is_redirected_to "home"post "/store/add_to_cart", :id => products(:lunchbox).idassert_response :successassert_not_nil assigns(:new_item)endprivatedef is_redirected_to(template)assert_response :redirectfollow_redirect!assert_response :successassert_template templateendend
Then you‘d run it by simply typing:
$ rake test:integration
Enjoy!
Comment | Permalink | Syndicate
Continued in the Archives...
2006 (15)April (2)March (5)February (5)January (3)
2005 (60)December (6)November (4)October (6)September (1)August (3)July (6)June (5)May (6)April (2)March (7)February (5)January (9)
2004 (33)December (3)November (3)October (2)September (5)August (3)July (2)June (2)May (1)March (4)February (4)January (4)
2003 (71)December (4)November (6)October (11)September (5)August (2)July (7)June (10)May (6)April (4)March (3)February (8)January (5)
2002 (25)December (5)November (9)October (11)
Copyright © 1999-2006 Clarkware Consulting, Inc.
All Rights Reserved
_xyz
Mike Clark‘s Weblog
Syndicate (RSS) | Archives
(Tue 04 Apr 2006) [/Rails] #
If you‘ve ever wanted to poke and prod your Rails app through the entire stack without firing up a web browser, Rails 1.1 just made it easy. You can now use the app object from within script/console to get access to theActionController::Integration::Session instance. And that means you can drive your app just like an integration test.
Let‘s take an app for a quick test drive:
$ script/consoleLoading development environment.>> app.class=> ActionController::Integration::Session
Going to Fred‘s home page should redirect him to a login page:
>> app.get "/home"=> 302>> app.controller.params=> {"action"=>"home", "controller"=>"accounts"}>> app.response.redirect_url=> "http://www.example.com/login"
Sure enough. Now let‘s log ol‘ Fred in:
>> app.post "/login", :user => {:login => ‘fred‘, :password => ‘flintstone‘}=> 302>> app.session[:user_id]=> 1>> app.cookies=> {"shopper_id"=>"176f8c0bf450f772f80f85ba22861504"}
Since Fred originally intended to hit his Home page, now that he‘s logged in he should have landed up back on his Home page with a cheery greeting:
>> app.response.redirect_url=> "http://www.example.com/home">> app.flash=> {:notice=>"Welcome Back, fred!"}
Being an avid shopper, Fred can‘t help but buy something. In this case, we use an asynchronous (yes, that‘s Ajaxy) request:
>> app.xml_http_request "/store/add_to_cart", :id => 1=> 200>> app.assigns(:new_item)=> #
require "#{File.dirname(__FILE__)}/../test_helper"class ShoppingTest < ActionController::IntegrationTestfixtures :productsdef test_shoppingget "/home"is_redirected_to "login"post "/login",:user => {:login => ‘fred‘, :password => ‘flintstone‘}is_redirected_to "home"post "/store/add_to_cart", :id => products(:lunchbox).idassert_response :successassert_not_nil assigns(:new_item)endprivatedef is_redirected_to(template)assert_response :redirectfollow_redirect!assert_response :successassert_template templateendend
Then you‘d run it by simply typing:
$ rake test:integration
Enjoy!
Comment | Permalink | Syndicate
Continued in the Archives...
2006 (15)April (2)March (5)February (5)January (3)
2005 (60)December (6)November (4)October (6)September (1)August (3)July (6)June (5)May (6)April (2)March (7)February (5)January (9)
2004 (33)December (3)November (3)October (2)September (5)August (3)July (2)June (2)May (1)March (4)February (4)January (4)
2003 (71)December (4)November (6)October (11)September (5)August (2)July (7)June (10)May (6)April (4)March (3)February (8)January (5)
2002 (25)December (5)November (9)October (11)
Copyright © 1999-2006 Clarkware Consulting, Inc.
All Rights Reserved
_xyz
Mike Clark‘s Weblog:Running Your Rails App Headless
Otaku, Cedric‘s weblog: Why Ruby on Rails won‘t become mainstream
Slobodan Celenkovic‘s Weblog : Weblog
Cero’s Weblog
Nio’s Weblog ? PHP & memcached
10 Ways To Create Content For Your Weblog
Integrate FCKEditor with your Ruby on Rails application
Integrate FCKEditor with your Ruby on Rails application
Windix‘s Weblog Reloaded: 汉字编码研究
再见安打---vick‘s weblog: 网络 Archives
L7-filter 安裝實錄 -- Jamyy‘s Weblog
Jamyy‘s Weblog: Linux 架站心得
Pengbone’s Weblog|如你所知
Xiaoxiao's Weblog - 如何提高悟性?
了解UUID - Mozier's weblog - 博客园
U.S. says time running out for Iran
部落格和分享.Share - Beardnan‘s Weblog
Netfilter Extensions: Patch-O-Matic -- Jamyy‘s Weblog
ASSP (Anti-Spam SMTP Proxy) 使用手札 -- Jamyy‘s Weblog
在 Linux 使用 PCMCIA 網卡 - 補充、備忘 -- Jamyy‘s Weblog
对Facebook的一些想法 - Xiaoxiao's Weblog
Linux Bonding (合併網卡) 實作 --Jamyy‘s Weblog
Sendmail: localhost.localdomain 的問題 -- Jamyy‘s Weblog
squid.conf ACL Tag 備忘 --Jamyy‘s Weblog