如何在 Rails 3 中使用 Rspec 2 测试路由?

发布于 2024-11-08 02:38:41 字数 70 浏览 0 评论 0原文

我找不到任何解释如何在 Rails 3 中测试路由的内容。即使在 Rspec 书中,它也没有很好地解释。

谢谢

I can't find anything explaining how to test routes in Rails 3. Even in the Rspec book, it doesn't explain well.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-11-15 02:38:41

rspec-rails Github 站点上有一个简短的示例。您还可以使用脚手架生成器来生成一些固定示例。例如,

rails gscaffold Article

应该生成如下内容:

require "spec_helper"

describe ArticlesController do
  describe "routing" do

    it "routes to #index" do
      get("/articles").should route_to("articles#index")
    end

    it "routes to #new" do
      get("/articles/new").should route_to("articles#new")
    end

    it "routes to #show" do
      get("/articles/1").should route_to("articles#show", :id => "1")
    end

    it "routes to #edit" do
      get("/articles/1/edit").should route_to("articles#edit", :id => "1")
    end

    it "routes to #create" do
      post("/articles").should route_to("articles#create")
    end

    it "routes to #update" do
      put("/articles/1").should route_to("articles#update", :id => "1")
    end

    it "routes to #destroy" do
      delete("/articles/1").should route_to("articles#destroy", :id => "1")
    end

  end
end

There is a brief example on the rspec-rails Github site. You can also use the scaffold generator to produce some canned examples. For instance,

rails g scaffold Article

should produce something like this:

require "spec_helper"

describe ArticlesController do
  describe "routing" do

    it "routes to #index" do
      get("/articles").should route_to("articles#index")
    end

    it "routes to #new" do
      get("/articles/new").should route_to("articles#new")
    end

    it "routes to #show" do
      get("/articles/1").should route_to("articles#show", :id => "1")
    end

    it "routes to #edit" do
      get("/articles/1/edit").should route_to("articles#edit", :id => "1")
    end

    it "routes to #create" do
      post("/articles").should route_to("articles#create")
    end

    it "routes to #update" do
      put("/articles/1").should route_to("articles#update", :id => "1")
    end

    it "routes to #destroy" do
      delete("/articles/1").should route_to("articles#destroy", :id => "1")
    end

  end
end
抠脚大汉 2024-11-15 02:38:41

Zetetic 的答案解释了如何测试路线。这个答案解释了为什么你不应该这样做。

一般来说,您的测试应该测试向用户(或客户端对象)公开的行为,而不是测试提供该行为的实现。路由是面向用户的:当用户输入 http://www.mysite.com/profile 时,他并不关心它是否会转到 ProfilesController;相反,他关心的是他能看到自己的个人资料。

因此,不要测试您是否要使用 ProfilesController。相反,设置一个 Cucumber 场景来测试当用户转到 /profile 时,他会看到他的姓名和个人资料信息。这就是你所需要的。

再次强调:不要测试你的路线。测试你的行为。

Zetetic's answer explains how to test routes. This answer explains why you shouldn't do that.

In general, your tests should test the behavior exposed to the user (or client object), not the implementation by which that behavior is provided. Routes are user-facing: when the user types in http://www.mysite.com/profile, he doesn't care that it goes to ProfilesController; rather, he cares that he sees his profile.

So don't test that you're going to ProfilesController. Rather, set up a Cucumber scenario to test that when the user goes to /profile, he sees his name and profile info. That's all you need.

Again: don't test your routes. Test your behavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文