1

我是测试新手,我很难弄清楚为什么我的测试不起作用。这是我的名为 child_user_selected_categories.feature 的功能文件

@no-database-cleaner
Feature: Managing selected_categories
  In order to use portal
  I want to create selected_categories for child user


  Scenario:  Creating a child selected_categories for a child user
    Given the database contains no test data
    And an user and sub user exists
    Given I am on the homepage

    When I attempt to sign in with following user account:
      | email address         | password |
      | abc@company1.com   | password |

    Then I should see "abc@company1.com" message on page
    When I follow "All Child Users"
    And I view "Child Categories" for sub user "xyztest@gmail.com"
    Then I should see "Default Filter Level for User xyztest@gmail.com is"
    When I press "APPLY"
    Then I should see "Custom Filter Created for xyztest@gmail.com"

我正在使用 factory_girl 插入记录。这是我的步骤定义文件 child_user_selected_categories.rb

And /^I view "([^"]*)" for sub user "([^"]*)"$/ do |link, user|
  selector = find('tr', text: user)
  within selector do
    click_link(link)
  end
end

Given(/^an user and sub user exists$/) do
  parent = FactoryGirl.create(:user)
  user = FactoryGirl.create(:user, email: "xyztest@gmail.com", parent_id: parent.id, default_filter_level: parent.default_filter_level)
  account = FactoryGirl.create(:account, user_id: parent.id)
  FactoryGirl.create(:filter, user_id: user.id, account_id: account.id)
  FactoryGirl.create(:filter, user_id: parent.id, account_id: account.id)
end

在 features/support 下的名为 user.rb 的文件中包含覆盖某些用户字段的代码需要 'factory_girl_rails'

FactoryGirl.define do
  factory :user do |u|
    u.email 'recdns@company1.com'
    u.password 'password'
    u.src_ip_addr 'hostname -I'
    u.ar_id true
    u.default_filter_level 'Medium'
    u.src_netmask '255.255.255.0'
  end
end

使用 jquery post 应用提交按钮

//on sibmit this will create a records for user for selected categories
        $("#selectes_categories").submit(function(){

            // get alll the selected category ids in list box 2
            var options = $("select#lstBox2").children().map(function() {return $(this).val();}).get();

            //var values = $('#categories_ids_').val()
            $('#selected_category_ids').val(options);
            $.post($(this).attr("action"),$(this).serialize(),function(response){
                $("#flash_notice").html(response);
            });
            return false;
        });

<%= form_tag "/levels/create_user_selected_categories", :id=> "selectes_categories" do %>
    <%= hidden_field_tag 'selected_category_ids' %>
    <%= hidden_field_tag 'user_id',@user.id %>
    <%= submit_tag "APPLY",:class => 'btn btn-primary'%>
<% end %>

在提交我的代码做它的工作。但是当我说按应用时它不能使用黄瓜。我已经为几乎相同的场景编写了测试,它们工作得很好,但唯一的区别是我在这里使用 jquery post 去控制器并创建记录。这是控制器代码

def create_user_selected_categories
    @user = User.find(params[:user_id]) if params[:user_id] != nil
    if @user != nil
      UserSelectedCategory.where(:user_id => @user.id).destroy_all
      @selected_user_categories = params[:selected_category_ids]
      @selected_user_categories.split(",").each{|catg_id|
        UserSelectedCategory.create(:user_id => @user.id, :category_id => catg_id.to_i)
      }
      @total_category_masks_for_user = Category.sum(:category_mask, :conditions => {:id => @user.user_selected_categories.map{|catg| catg.category_id}})

      @user_filter = Filter.find_by_user_id(@user.id)
      if @user_filter == nil
        Filter.create(:category_mask => @total_category_masks_for_user, :account_id => @user.account.id, :user_id => @user.id)
      else
        @user_filter.category_mask = @total_category_masks_for_user
        @user_filter.save
      end

    end
    render :action => 'create_user_selected_categories', :layout => false
  end

有什么建议么?谢谢

4

1 回答 1

1

假设您使用 Capybara 和 Cucumber,默认情况下不会执行您的 Javascript。一个非常简单的解决方案可能是将@javascript标签添加到您的场景中:

@javascript 
Scenario:  Creating a child selected_categories for a child user
...

这将使用Selenium驱动程序运行您的场景,该驱动程序实际上将打开一个浏览器窗口并像真实用户一样测试您的应用程序,其中包括 Javascript 执行。默认情况下,它将尝试使用 Firefox,因此请确保您已安装。

更多:https ://github.com/jnicklas/capybara#using-capybara-with-cucumber

于 2013-05-31T20:25:22.710 回答