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

ルーティングのテスト

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 年 7 月 6 日 12:44 | #1

    I needed to thank you for this fantastic read!! I definitely enjoyed every bit of it. I’ve got you book marked to look at new stuff you post…

  2. 2020 年 7 月 6 日 12:46 | #2

    I¡¦ve recently started a web site, the information you offer on this website has helped me greatly. Thank you for all of your time & work.

  3. 2020 年 7 月 6 日 12:48 | #3

    I wanted to thank you for this excellent read!! I certainly enjoyed every little bit of it. I’ve got you bookmarked to look at new things you post…

  4. 2020 年 7 月 6 日 12:50 | #4

    You ought to take part in a contest for one of the finest blogs online. I will recommend this web site!

  5. 2020 年 7 月 6 日 12:52 | #5

    You made some really good points there. I checked on the web for more info about the issue and found most people will go along with your views on this site.

  6. 2020 年 7 月 6 日 12:53 | #6

    I could not refrain from commenting. Perfectly written!

  7. 2020 年 7 月 6 日 13:01 | #7
  8. 2020 年 7 月 6 日 13:06 | #8

    Hello, There’s no doubt that your website may be having internet browser compatibility issues. Whenever I take a look at your website in Safari, it looks fine however, when opening in I.E., it’s got some overlapping issues. I simply wanted to give you a quick heads up! Apart from that, excellent blog!

  9. 2020 年 7 月 6 日 13:08 | #9

    Your style is so unique compared to other people I have read stuff from. Thanks for posting when you’ve got the opportunity, Guess I will just bookmark this web site.

  10. 2020 年 7 月 6 日 13:11 | #10

    Way cool! Some extremely valid points! I appreciate you penning this write-up and also the rest of the website is also really good.

  11. 2020 年 7 月 6 日 13:13 | #11

    Excellent site you’ve got here.. It’s difficult to find excellent writing like yours these days. I honestly appreciate people like you! Take care!!

  12. 2020 年 7 月 6 日 13:16 | #12

    This website was… how do I say it? Relevant!! Finally I have found something that helped me. Thanks a lot!

  13. 2020 年 7 月 6 日 13:19 | #13

    An outstanding share! I’ve just forwarded this onto a co-worker who had been doing a little research on this. And he actually ordered me dinner due to the fact that I stumbled upon it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanks for spending time to discuss this topic here on your web site.

  14. 2020 年 7 月 6 日 13:22 | #14

    I’m impressed, I have to admit. Rarely do I come across a blog that’s both equally educative and entertaining, and let me tell you, you have hit the nail on the head. The problem is something which not enough men and women are speaking intelligently about. I am very happy that I stumbled across this during my hunt for something regarding this.

  15. 2020 年 7 月 6 日 13:23 | #15

    You should be a part of a contest for one of the finest blogs online. I will highly recommend this website!

  16. 2020 年 7 月 6 日 13:25 | #16

    After I originally commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I receive four emails with the same comment. Perhaps there is a way you are able to remove me from that service? Thanks a lot!

  17. 2020 年 7 月 6 日 13:31 | #17

    Hello, Neat post. There’s a problem with your website in internet explorer, may test this¡K IE nonetheless is the marketplace leader and a good component to folks will pass over your excellent writing because of this problem.

  18. 2020 年 7 月 6 日 13:32 | #18

    After looking over a number of the articles on your web page, I really appreciate your technique of writing a blog. I added it to my bookmark site list and will be checking back in the near future. Take a look at my website as well and tell me how you feel.

  19. 2020 年 7 月 6 日 13:35 | #19

    You have made some really good points there. I looked on the net for additional information about the issue and found most people will go along with your views on this web site.

  20. 2020 年 7 月 6 日 13:39 | #20

    Having read this I thought it was really informative. I appreciate you taking the time and energy to put this content together. I once again find myself spending way too much time both reading and leaving comments. But so what, it was still worthwhile!

  21. 2020 年 7 月 6 日 13:40 | #21

    Greetings! Very useful advice within this article! It’s the little changes that will make the most important changes. Thanks for sharing!

  22. 2020 年 7 月 6 日 13:41 | #22

    This is a topic that is close to my heart… Thank you! Exactly where are your contact details though?

  23. 2020 年 7 月 6 日 13:43 | #23

    I would like to thank you for the efforts you’ve put in writing this website. I am hoping to see the same high-grade content from you in the future as well. In truth, your creative writing abilities has encouraged me to get my very own website now ;)

  24. 2020 年 7 月 6 日 13:46 | #24

    I’m impressed, I have to admit. Seldom do I come across a blog that’s both equally educative and entertaining, and let me tell you, you have hit the nail on the head. The issue is something too few folks are speaking intelligently about. Now i’m very happy that I stumbled across this in my hunt for something relating to this.

  25. 2020 年 7 月 6 日 13:46 | #25

    This page certainly has all the information I needed concerning this subject and didn’t know who to ask.

  26. 2020 年 7 月 6 日 13:56 | #26

    I am not certain where you are getting your information, but great topic. I must spend some time learning more or understanding more. Thank you for wonderful information I used to be on the lookout for this info for my mission.

  27. 2020 年 7 月 6 日 13:58 | #27

    An interesting discussion is worth comment. There’s no doubt that that you ought to publish more on this subject matter, it may not be a taboo matter but usually folks don’t speak about such issues. To the next! Many thanks!!

  28. 2020 年 7 月 6 日 14:00 | #28

    You made some good points there. I looked on the internet for additional information about the issue and found most people will go along with your views on this site.

  29. 2020 年 7 月 6 日 14:00 | #29

    You should take part in a contest for one of the greatest sites on the internet. I’m going to recommend this website!

  30. 2020 年 7 月 6 日 14:02 | #30
コメントページ
1 254 255 256 389
  1. 2020 年 4 月 14 日 18:25 | #1
  2. 2020 年 4 月 20 日 10:04 | #2
  3. 2020 年 4 月 30 日 12:33 | #3
  4. 2020 年 5 月 5 日 15:03 | #4
  5. 2020 年 5 月 5 日 18:45 | #5
  6. 2020 年 5 月 9 日 18:45 | #6
  7. 2020 年 7 月 1 日 21:54 | #7