【问题标题】:Cucumber can't find routes? This is potentially a big newb question黄瓜找不到路线?这可能是一个很大的新手问题
【发布时间】:2010-08-14 21:10:07
【问题描述】:

带有 Rspec2 和 Cucumber 的 Rails3 应用

黄瓜

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!

对不起,我太新了。这是我第一次尝试黄瓜。 :(

【问题讨论】:

    标签: ruby-on-rails ruby rspec ruby-on-rails-3 cucumber


    【解决方案1】:

    Rails 3 路线中不推荐使用 first off map,您可能应该有这样的东西。

    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 = 'example@example.com'
      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),因此您可以使用visit path_to(page_name)。

    【讨论】:

    • Righteo。我想到了。非常感谢。我敢肯定,从现在到成为一个更小的新手,我会陷入十几个陷阱;)。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多