ホーム > 今日のテストコード > ルーティングのテスト

ルーティングのテスト

railsのルーティングのテスト。
:id => /\d+/ と指定している部分「これじゃ部分一致では?」と思いましたが、ActionController::Routing::Route#generation_requirements で\Aと\Zで挟まれて再コンパイルされてますね。

config/routes.rb

1
2
3
4
5
6
ActionController::Routing::Routes.draw do |map|
  map.with_options(:controller => 'products') do |products|
    products.connect 'products/:id', :action => 'show', :id => /\d+/
    products.connect 'products', :action => 'create', :conditions => { :method => :post }
  end
end

spec/controllers/products_controller_spec.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'spec_helper'
 
describe ProductsController do
 
  describe 'ルーティングのテスト' do
 
    it 'Products#show' do
      params_from(:get, '/products/123').should == { 
        :controller => 'products',
        :action => 'show',
        :id => '123'
      }
    end
 
    it 'idが数字のみでない場合は404' do
      lambda { 
        params_from(:get, '/products/123abc')
      }.should raise_error(ActionController::RoutingError)
    end
 
    it 'Product#create' do
      params_from(:post, '/products').should == { 
        :controller => 'products',
        :action => 'create'
      }
    end
 
    it 'createはPOSTのみ' do
      lambda { 
        params_from(:get, '/products')
      }.should raise_error(ActionController::MethodNotAllowed)
    end
 
  end
 
end

参考:

  1. 2020 年 4 月 1 日 19:55 | #3

    Spot on with this write-up, I truly believe this amazing site needs much more attention. I’ll probably be returning to see more, thanks for the advice!

  2. 2020 年 4 月 1 日 21:25 | #5
  3. 2020 年 4 月 1 日 22:23 | #7
  4. 2020 年 4 月 1 日 23:33 | #9
  5. 2020 年 4 月 2 日 03:03 | #12

    Oh my goodness! Amazing article dude! Thank you so much, However I am experiencing difficulties with your RSS. I don’t know the reason why I can’t subscribe to it. Is there anybody else having similar RSS issues? Anyone that knows the answer can you kindly respond? Thanx!!

  6. 2020 年 4 月 2 日 05:47 | #17

    After I initially left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and now whenever a comment is added I receive four emails with the exact same comment. Perhaps there is a means you can remove me from that service? Cheers!

  7. 2020 年 4 月 2 日 09:57 | #24

    Nice post. I learn something totally new and challenging on sites I stumbleupon on a daily basis. It will always be exciting to read articles from other authors and use something from other sites.

  8. 2020 年 4 月 2 日 09:58 | #25

    Very good article! We will be linking to this particularly great article on our website. Keep up the good writing.

  9. 2020 年 4 月 2 日 10:20 | #26

    I’m impressed, I have to admit. Seldom do I come across a blog that’s both equally educative and interesting, and let me tell you, you have hit the nail on the head. The issue is something which too few men and women are speaking intelligently about. I’m very happy I stumbled across this during my search for something relating to this.

  10. 2020 年 4 月 2 日 12:59 | #27

    I truly love your site.. Excellent colors & theme. Did you build this amazing site yourself? Please reply back as I’m wanting to create my very own website and would love to learn where you got this from or what the theme is called. Kudos!

  11. 2020 年 4 月 2 日 14:22 | #29

    I really like it when people come together and share views. Great blog, continue the good work!

コメントページ
1 18 19 20 389
  1. トラックバックはまだありません。