Skip to content

Instantly share code, notes, and snippets.

@kurotaky
Created November 23, 2012 20:37
Show Gist options
  • Save kurotaky/4137175 to your computer and use it in GitHub Desktop.
Save kurotaky/4137175 to your computer and use it in GitHub Desktop.
rspec-rails: route_to matcher のなぞ
# expect to の書き方が推奨されているので書きなおす
# https://github.com/rspec/rspec-rails
# ↓ 落ちる
it "routes to #show" do
expect(get: "/entries/1").to route_to(controller: "entries", action: "show", id: "1")
end
Failures:
1) EntriesController routing routes to #show
Failure/Error: expect(get: "/entries/1").to route_to(controller: "entries", action: "show", id: "1")
NoMethodError:
undefined method `message' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f9a24f4b008>
# ./spec/routing/entries_routing_spec.rb:8:in `block (3 levels) in <top (required)>'
#
# ↓ 落ちるテスト1
#
# -*- coding: utf-8 -*-
require "spec_helper"
describe EntriesController do
describe "routing" do
it "routes to #show" do
get("/entries/1").should route_to("entries#show", :id => "1")
end
it "routes to #edit" do
get("/entries/1/edit").should route_to("entries#edit", :id => "1")
end
it "routes to #create" do
post("/entries").should route_to("entries#create")
end
it "routes to #update" do
put("/entries/1").should route_to("entries#update", :id => "1")
end
it "routes to #destroy" do
delete("/entries/1").should route_to("entries#destroy", :id => "1")
end
end
end
# 落ちるテスト2
#
# このように書いたら落ちる https://www.relishapp.com/rspec/rspec-rails/v/2-3/docs/routing-specs/route-to-matcher
# ↓
it "routes to #show" do
{get: "/entries/1"}.
should route_to(controller: "entries", action: "show", id: "1")
end
it "routes to #create" do
{post: "/entries"}.should route_to(controller: "entries", action: "create")
end
Failures:
1) EntriesController routing routes to #show
Failure/Error: {get: "/entries/1"}.
NoMethodError:
undefined method `message' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007fe990aad378>
# ./spec/routing/entries_routing_spec.rb:8:in `block (3 levels) in <top (required)>'
2) EntriesController routing routes to #create
Failure/Error: {post: "/entries"}.should route_to(controller: "entries", action: "create")
NoMethodError:
undefined method `message' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007fe990b0bb80>
# ./spec/routing/entries_routing_spec.rb:18:in `block (3 levels) in <top (required)>'
Finished in 0.02782 seconds
6 examples, 2 failures
#
# テストが通る書き方1
# ↓
#
# -*- coding: utf-8 -*-
require "spec_helper"
describe EntriesController do
describe "routing" do
it "routes to #show" do
{get: "/entries/1"}.should
route_to(controller: "entries", action: "show", id: "1")
end
it "routes to #edit" do
{get: "/entries/1/edit"}.should
route_to(controller: "entries", action: "edit", id: "1")
end
it "routes to #create" do
{post: "/entries"}.should
route_to(controller: "entries", action: "create")
end
it "routes to #update" do
{put: "/entries/1"}.should
route_to(controller: "entries", action: "update", id: "1")
end
it "routes to #destroy" do
{delete: "/entries/1"}.should
route_to(controller: "entries", action: "destroy", id: "1")
end
end
end
# テスト実行
/Users/yuta/rails_projects/mameblo% be rspec spec/routing/ (git)-[rails4-edge]
No DRb server is running. Running in local process instead ...
BrothersController
routing
routes to #create
EntriesController
routing
routes to #show
routes to #edit
routes to #create
routes to #update
routes to #destroy
Finished in 0.01182 seconds
6 examples, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment