Rails 4.x在浏览器中工作,但Cucumber步骤失败

时间:2014-10-12 19:35:43

标签: ruby-on-rails cucumber

我正在开发一个小型企业客户管理应用程序。

我的代码似乎工作正常,但我的黄瓜测试有以下信息。

  And I have created one business      # features/step_definitions/owner_steps.rb:238
  No route matches {:action=>"new", :business_id=>nil, :controller=>"customers"} missing required keys: [:business_i
d] (ActionView::Template::Error)
  ./app/views/owners/show.html.erb:64:in `_app_views_owners_show_html_erb__1329160335005789747_57615300'
  ./features/step_definitions/owner_steps.rb:240:in `/^I have created one business$/'
  features/business_creates_customers.feature:9:in `And I have created one business'

再一次,当我转到页面时它会起作用。我相信这个怪癖就在于我将如何进入new_business_customers_path。我的“默认”页面是所有者显示页面。业主拥有很多业务。但是,业务有很多客户。要访问客户页面并从所有者显示页面输入正确的business_id,我有一种方法可以在所有者控制器中找到正确的业务,并且它可以在浏览器中正确传递参数。

无论如何,这是我的代码:

所有者(show.html.erb) - 请注意new_business_customer_path(@business)。当我放@owner,@ business时,它将OWNER ID作为business_id提供

<%= provide(:title, "#{@owner.name} Profile Page") %>
<div class="row">
    <div class="col-md-2">

    <div class="panel panel-default">
        <div class="panel-heading">
        <b>Personal Information</b><%= link_to "Edit", '/edit', class: 'pull-right' %>
      </div>
      <div class="panel-body">
        <b>Owner: </b>
        <%= link_to @owner.name, '/edit' %>
      <p>
        <b>Email: </b>
        <%= @owner.email %>
      </div>

      <% if @owner.businesses.count > 0 %>
      <div class="panel-heading">
        <b>Business Information</b><%= link_to "Add", new_owner_business_path(@owner), class: 'pull-right' %>
      </div>      
      <div class="panel-body">
        <% @owner.businesses.order("created_at asc").each do |business| %>
          <b>View:</b>
          <%= link_to business.name, owner_business_path(@owner, business) %><br>
        <% end %>
      </div>
      <% end %>

      </div>
  </div>

    <div class="col-md-10">
  <% if @owner.businesses.count == 0 %>
    <div class="panel panel-danger">
      <div class="panel-heading">
        <b>Business Information</b>
      </div>
      <div class="panel-body">
        <p>Thank you for signing up.   We will need information about your business for you to proceed.</p>
        <p>You have not yet provided us information about your business.</p>
        <p><%= link_to "Add your business now!", new_owner_business_path(@owner) %></p>
      </div>
    </div>
  <% else %>
    <div class="panel panel-danger">
      <div class="panel-heading">
        <b>Services Information</b>
      </div>
      <div class="panel-body">
      <p>
       Ok.  Your business is created.   We're about to add customers, which is kind of why you're here.  But, before we can do that, we need to know what types of services you provide them when they are with you.  You've already categorized your business when you created it.  Similar businesses who have already populated their services will be available options for you when you add here.  You can always add more, but we want you to start by adding at least one service now.  Please tell us what types of services your business provides.
       </p>
       <p><%= link_to "Add a service your business provides now!", '#' %></p>
      </div>
    </div>

    <div class="panel panel-danger">
      <div class="panel-heading">
        <b>Customer Information</b>
      </div>
      <div class="panel-body">
        <p>
          You have not yet added any customers.
        </p>
        <p><%= link_to "Add a customer for your business now!", new_business_customer_path(@business) %></p>
      </div>
    </div>
  <% end %>

  </div>

</div>

黄瓜测试本身。它在第三步失败了。

Feature: Business creates customers
    In order to conduct business
    my business should be able to add customers
    I can create as an owner of the business

    Background: Logging in with a business
        Given I am logged in
      And I am at my owner profile page
      And I have created one business

      Scenario: Creating first customer
        Then I expect to see content "Customer Information"
        And I expect to see content "You have not yet added any customers."
        And I expect to see a link to "Add a customer for your business now!"
        When I click the "Add a customer for your business now!" link
        Then I am at the create new customer page
        And I expect to see a form to add customer information
        And I expect to see the title "Create a customer"
        When I fill in "First name" with "Kathy"
        And I fill in "Middle name" with ""
        And I fill in "Last name" with "Davis"
        And I fill in "Email" with "test@test.com"
        And I fill in "Phone number" with "651.555.1212"
        And I fill in "Referred by" with ""
        When I click the "Save Customer" button
        Then I expect to see content "This customer has been created."
        And I am at my owner profile page
        Then I expect to see content "Customer Information"
        But I expect to not see content "You have not yet added any customers."
        Then I expect to see a link to "Customers"
        And I expect to see a link to "Add"
        And I expect to see content "Customers = 1"
        And I expect to see a link to "Add customers"

Customers_controller.rb - 此中的定义将不会获得:business_id从上一页传递,因为它是所有者显示页面,我正在将@business推送给所有者。我想这可能是我的问题。

class CustomersController < ApplicationController
    before_action :get_business_and_owner

    def new
        @customer = Customer.new
    end

    private

            def get_business_and_owner
                @business = Business.find(params[:business_id])
                @owner = @business.owner
            end

end

owners_controller.rb - 这里我在“selected_business”的Owners模型中有一个特殊的方法来获取已选择布尔值的业务。所以它确实知道它的业务ID,但不能在测试中传递business_id,但实际上可以。

class OwnersController < ApplicationController
  before_action :set_owner, only: [:show, :edit, :update, :destroy]
  before_action :set_business, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource


    def show
    end



    private
    def owner_params
      params.require(:owner).permit(:first_name, :last_name, :middle_name, :email, :password, :password_confirmation)
    end

        def set_owner
            @owner = Owner.find(params[:id])
        end 

        def set_business
            @business = @owner.selected_business
        end

end

我可以投入更多,但我认为这些就是那些。我很确定我在上面的步骤定义中并没有完全正确地做某事。在那里我做了以下代码。

def create_business
  @business = @owner.businesses.create(name: "My Great Business", description: "Cool business, huh?")
end

然后我访问root。而且我很确定我只需要找到一种方法来以某种方式识别此测试中的所选业务。但是编写一个选定的业务方法似乎并不适用于那里。只是让我疯了,所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

如怀疑的那样,问题是我是一个菜鸟:)。

在测试中,我没有真正测试应用程序的流程。我按预期登录并到达所有者个人资料页面。然后我的创建步骤将一个企业写入数据库,这工作得很好,但没有告诉应用程序任何东西,所以视图逻辑不会改变。因此,测试中没有传递任何内容,但我可以看到它在视图中传递。

我试图通过期望当前页面是X页来解决这个问题,但是,这也试图根据我的方式强制@variable用于测试中不存在的业务。写了测试。一旦我记得让应用程序完成工作,就会按预期工作。

我看到这被投了几次,​​所以我很抱歉,如果这个问题太过正常了。我很感激人们投票时的任何反馈,这样我才能学得更好。谢谢大家。