我是新來測試,我很難找出爲什麼我的測試不工作。這裏是我的功能文件叫做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 |
| [email protected] | password |
Then I should see "[email protected]" message on page
When I follow "All Child Users"
And I view "Child Categories" for sub user "[email protected]"
Then I should see "Default Filter Level for User [email protected] is"
When I press "APPLY"
Then I should see "Custom Filter Created for [email protected]"
我使用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: "[email protected]", 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
一篇名爲下功能user.rb /支持有代碼overrite一些用戶領域 需要「factory_girl_rails」
FactoryGirl.define do
factory :user do |u|
u.email '[email protected]'
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 %>
關於提交我的代碼的工作。但是當我說按APPLY時,它不在使用黃瓜。我已經寫了幾乎相同的情況測試,他們工作得很好,但唯一的區別是在這裏我使用jQuery帖子去控制器和創建記錄。這裏是控制器代碼
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
有什麼建議嗎?謝謝
我加入@javascript,現在它說'當我嘗試使用下面的用戶帳戶登錄:#功能/ step_definitions/user_steps.rb:65 |電子郵件地址|密碼| | [email protected] |密碼| 無法找到字段「電子郵件」(Capybara :: ElementNotFound)'這是user_steps.rb代碼'給定/ ^我嘗試使用電子郵件登錄「(。*?)」,密碼「(。*?)」$ /做|電子郵件,密碼| 登錄(電子郵件,密碼) end''DEF登錄(電子郵件,密碼) FILL_IN( 'USER_EMAIL',:與=>電子郵件) FILL_IN( 'USER_PASSWORD',:與=>密碼) click_button( '登錄' ) end' – user588324
它看起來像你的測試試圖運行登錄步驟,但瀏覽器不在正確的頁面上。您應該能夠觀看瀏覽器並查看它正在導航的頁面,以確保它是正確的。 您也可以在''我在主頁上'的步驟的最後加上'save_and_open_page',這將打開您的測試導航到的頁面的「快照」。 –