为什么这个黄瓜代码失败了

时间:2012-11-04 20:47:25

标签: ruby-on-rails-3 cucumber

我有这段代码:https://github.com/roelof1967/tamara 但是当我做黄瓜时,它会失败并显示以下错误消息:https://gist.github.com/4008123

任何人都可以向我解释我的代码有什么问题吗?

鲁洛夫

编辑1: 代码失败:

class Output  
def messages    
   @messages ||= []  
end    
def puts(message)    
    messages << message  
end    
def output    
   @output ||= Output.new  
end
end

When /^he logs in$/ do   
   visit("/users/sign_in")  
   fill_in('Email', :with => @user.email)  
   fill_in('Password', :with => @user.password)  
   click_button('Sign in')
end

Then /^he should see "(.*?)"$/ do |message|  
  @output.messages.should include (message)
end

以下是错误消息:

undefined method `[]' for nil:NilClass NoMethodError)      
./features/step_definitions/login_steps.rb:6:in `/^he logs in$/'          
features/login.feature:5:in `When he logs in'

1 个答案:

答案 0 :(得分:0)

为您的解决方案执行此操作:

第1步:创建家庭控制器

class HomeController < ApplicationController
  def index
  end
end

第2步:在home中创建 view 文件夹,然后创建 index.html.erb 文件内容看起来像一个例子

<% if current_user.present? %>
  <h1> <%= link_to "Sign Out", destroy_user_session_path, :method => :delete %> </h1>
<% else %>
  <h1> <%= link_to "Sign Up", "/users/sign_up" %> |<%= link_to "Sign In", user_session_path %> </h1>
<% end %> 

第3步:现在您的 login.feature 文件看起来像

Feature: login and authecation

Scenario: log in as existing user
  Given a user "roelof" exists
  When he logs in
  Then he should see "Signed in successfully."
  And he should see "Sign Out"

Scenario: log in with bad password
  Given a user "Aslak" exists
  When he logs in with a bad password
  Then he should not see "Welcome, Aslak"
  And he should see "Login failed"

第4步现在,运行cucumber

您的方案“以现有用户身份登录”将通过。