黄瓜找不到路线?这可能是一个新的大问题

发布于 2024-09-14 05:24:24 字数 1118 浏览 1 评论 0原文

使用 Rspec2 和 Cucumber 的 Rails3 应用程序

Cucumber

Given /^that a user is logged in$/ do
  current_user = User.first
  render new_user_post_path(current_user)
end

Routes.rb

map.resources :users do |users|
  users.resources :posts, :collection => {:view => :get}
end

posts_controller_spec

describe PostsController do
  describe "#new" do
    it "should be successful" do
      get :new
      response.should be_success
    end
  end
end

我的第一个史诗般的失败

(::) failed steps (::)

No route matches {:controller=>"posts", :action=>"new"} (ActionController::RoutingError)
./features/step_definitions/tasklist_steps.rb:3:in `/^that a user is logged in$/'
features/tasklist.feature:7:in `Given that a user is logged in'

Failing Scenarios:
cucumber features/tasklist.feature:6 # Scenario: List SubmitLink

1 scenario (1 failed)
3 steps (1 failed, 2 skipped)
0m0.147s
rake aborted!

抱歉,我也是纽布。这是我第一次尝试黄瓜。 :(

Rails3 app with Rspec2 and Cucumber

Cucumber

Given /^that a user is logged in$/ do
  current_user = User.first
  render new_user_post_path(current_user)
end

Routes.rb

map.resources :users do |users|
  users.resources :posts, :collection => {:view => :get}
end

posts_controller_spec

describe PostsController do
  describe "#new" do
    it "should be successful" do
      get :new
      response.should be_success
    end
  end
end

My first big epic Fail

(::) failed steps (::)

No route matches {:controller=>"posts", :action=>"new"} (ActionController::RoutingError)
./features/step_definitions/tasklist_steps.rb:3:in `/^that a user is logged in$/'
features/tasklist.feature:7:in `Given that a user is logged in'

Failing Scenarios:
cucumber features/tasklist.feature:6 # Scenario: List SubmitLink

1 scenario (1 failed)
3 steps (1 failed, 2 skipped)
0m0.147s
rake aborted!

Sorry, I'm too newb. This is my first ever attempt at cucumber. :(

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

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

发布评论

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

评论(1

蓝眼睛不忧郁 2024-09-21 05:24:24

首先,Rails 3 路线中不推荐使用地图,您可能应该有这样的东西。

resources :users do 
    resources :posts
end

按照我通常编写给定的方式,我遵循用户实际采用的路径,在测试数据库中创建用户,然后实际转到登录页面并登录,以便应用程序中的所有内容都得到正确设置。

Given /^I have one user "([^\"]*)" with email "([^\"]*)" and password "([^\"]*)"$/ do |username,email, password|
  @user = User.new(:email => email,
                   :username=>username,
                   :password => password,
                   :password_confirmation => password)
   @user.save!
end

Given /^I am an authenticated user$/ do
  name = 'exmample'
  email = '[email protected]'
  password = 'secret!'

  Given %{I have one user "#{name}" with email "#{email}" and password "#{password}"}
  And %{I go to the user login page}
  And %{I fill in "user_username" with "#{name}"}
  And %{I fill in "user_password" with "#{password}"}
  And %{I press "Sign in"}
end

我按以下方式使用它:

Feature: 
  In order to reboot my crappy/POS equipment without bothering the awesome Noc Monkeys
  I would like to login to a webpage and see the status of and control the outlets on my equipment

  Background: Valid and authenticated user with at least one outlet to control
    Given I am an authenticated user

  @ok
  Scenario: Viewing All outlets
    Given I am able to control an outlet with index "1"
    And I am on the home page
    Then I should see "server_1"

另外,通常我不会在黄瓜步骤中调用渲染。由于您正在使用模拟浏览器(假设是 webrat/capybara),您将访问 path_to(page_name)

Well first off map is deprecated in Rails 3 routes, you should probably have something like this.

resources :users do 
    resources :posts
end

The way I normally write my givens, I follow the path that the user would actually take, create the user in the test db, and then actually going to the login page and signing in, so everything gets set correctly in the app.

Given /^I have one user "([^\"]*)" with email "([^\"]*)" and password "([^\"]*)"$/ do |username,email, password|
  @user = User.new(:email => email,
                   :username=>username,
                   :password => password,
                   :password_confirmation => password)
   @user.save!
end

Given /^I am an authenticated user$/ do
  name = 'exmample'
  email = '[email protected]'
  password = 'secret!'

  Given %{I have one user "#{name}" with email "#{email}" and password "#{password}"}
  And %{I go to the user login page}
  And %{I fill in "user_username" with "#{name}"}
  And %{I fill in "user_password" with "#{password}"}
  And %{I press "Sign in"}
end

I use it the following way:

Feature: 
  In order to reboot my crappy/POS equipment without bothering the awesome Noc Monkeys
  I would like to login to a webpage and see the status of and control the outlets on my equipment

  Background: Valid and authenticated user with at least one outlet to control
    Given I am an authenticated user

  @ok
  Scenario: Viewing All outlets
    Given I am able to control an outlet with index "1"
    And I am on the home page
    Then I should see "server_1"

Also, normally I don't call render inside a cucumber step. Since you are working with a simulated browser (assuming webrat/capybara) you would visit path_to(page_name).

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