我是 ror 新手,我一直坚持在 Capybara 中编写测试。我需要做的是通过用户单击按钮来测试新案例的创建。但是当我检查眼镜时并没有看到创建。
require 'spec_helper'
feature "User can create new case" to
background do
@ user = FactoryGirl.create (: sysadmin_user) @ user.make_current
@plan = FactoryGirl.create(:plan)
@ account = FactoryGirl.create (: account, plan: @plan, user: @user)
@trial = FactoryGirl.create trial (:trial, account: @account)
@ trial.make_current
@ trial_phase = FactoryGirl.create (: trial_phase, trial: @trial)
login (@ user.email, @ user.password)
end
scenario 'create new case' to
Case.where puts ("created_by =?", @ user.id).count #returns 0
fill_in 'case_subject_identifier', with 'MyNewTest'
expect {click_button ('Create New Case')}.to change(Case,:count).by(1)
Case.where puts ("created_by =?", @ user.id).count #returns 0
end
结尾
这是控制器:
POST /cases 作为 HTML
def create`
@case = Case.where("subject_identifier = ? && trial_id = ?", params[:case]
[:subject_identifier], @current_trial.id).first
if @case || params[:case][:subject_identifier].blank?
respond_to :js
else`
params[:case][:subject_identifier]
=sanitize_value(params[:case[:subject_identifier])
if params[:case]
@case = Case.create(params[:case])``
if current_user.trial_member?(@current_trial, current_user)
@case.update_attributes(:location_id => current_user.default_location_id)
else
@case.update_attributes(:location_id => Location.find_by_name("None").id)
end
@case.update_attributes(:trial_phase_id
=>TrialPhase.find_by_name("None").id,:access
=> "None")
#redirect_to "/trial/#{@current_trial.id.to_s}/cases/#{@case.id.to_s}/case_view"
respond_to do |format|
format.js { render :js => %(window.location.pathname='#{'/trial
/'+@current_trial.id.to_s+'/cases/'+@case.id.to_s+'/case_view'}') }
end
end
end