【问题标题】:Rspec fill_in update giving errorsRspec fill_in 更新给出错误
【发布时间】:2014-08-02 02:09:02
【问题描述】:

我已经在这两天了,我已经用尽了所有的问题。所以希望 SO 可以提供帮助。

我的 user_pages_spec.rb 中有以下代码

describe "edit" do
  let(:org_person) {FactoryGirl.create(:org_person)}
  let(:org_credential) { FactoryGirl.create(:org_credential, :org_person_id => org_person.id ) }
  let(:user) { FactoryGirl.create(:org_contact) }
  before do
    session_sign_in org_credential
    visit edit_org_person_path(org_credential.org_person_id)
  end

  describe "page" do
    it { should have_content('Update your profile') }
    it { should have_title('Edit user') }
    it { should have_css('ul.nav-sidebar', count:3 )}
  end

  describe "with valid information" do

    let(:address1)  { "1234 Fake Street" }
    let(:address2)  { "Unit 95B" }
    let(:new_city) { "Toronto" }
    let(:new_country) { "124" } #country is an id of the corresponding record
    let(:new_region) { "527" } #region is an id of the corresponding record
    let(:new_postal) { "T4M 5R4" }
    let(:new_email) { "donny@example.com" }
    let(:new_business_number) { "60445612344" }
    let(:new_cell_number) { "7451354545" }

    before do
      fill_in "Address 1",            with: address1
      fill_in "Address 2",            with: address2
      fill_in "City",                 with: new_city
      have_select('Country', :selected => :new_country)
      have_select('Region', :selected => :new_region)
      fill_in "Postal Code",               with: new_postal
      fill_in "Email",                with: new_email
      fill_in "Business Number",      with: new_business_number
      fill_in "Cell Number",          with: new_cell_number
      click_button "Save Changes"
    end

    it { should have_title("Edit user") }
    it { should have_selector('div.alert.alert-success') }
    it { should have_link('Sign out', href: signout_path) }
    specify { expect(user.reload.address1).to  eq address1 }
    specify { expect(user.reload.address2).to eq address2 }
    specify { expect(user.reload.city).to  eq new_city }
    specify { expect(user.reload.typ_country_id).to  eq new_country }
    specify { expect(user.reload.typ_region_id).to  eq new_region }
    specify { expect(user.reload.postal_code).to  eq new_postal }
    specify { expect(user.reload.email).to  eq new_email }
    specify { expect(user.reload.business_number).to  eq new_business_number }
    specify { expect(user.reload.cell_number).to  eq new_cell_number }
  end

以下是我在 org_people_controller.rb 中的编辑和更新功能

 def edit
   @person = OrgPerson.find(params[:id])
   @contactInfo = OrgContact.find_or_create_by(org_person_id: params[:id]).attributes
   @person.org_contacts.build(@contactInfo)
 end

 def update
   # Create org_ca to sanitize our hash to proper "contacts" attributes
   @org_ca = update_person_params["org_contacts_attributes"]["0"]
   @org_ca[:typ_country_id] = @org_ca.delete :typ_countries
   @org_ca[:typ_country_id] = @org_ca[:typ_country_id][:id]
   @org_ca[:typ_region_id] = @org_ca.delete :typ_regions
   @org_ca[:typ_region_id] = @org_ca[:typ_region_id][:id]

   # Edit function variables, in case of failed validations and we re-render :edit
   @person = OrgPerson.find_by(id: @org_ca["org_person_id"])
   @contactInfo = OrgContact.find_or_create_by(org_person_id:      @org_ca["org_person_id"]).attributes
   @person.org_contacts.build(@contactInfo)

   # Find contacts and credentials record or create them if necessary
   @contact = OrgContact.find_or_initialize_by(org_person_id: @org_ca["org_person_id"])
   @credential = OrgCredential.find_or_initialize_by(org_person_id: @org_ca["org_person_id"])

   # Try to save it, if it saves, then redirect to the edit page with success
   if @contact.update_attributes(@org_ca) &&  @credential.update_attribute(:user_name, @org_ca["email"])
     flash[:success] = "Profile updated"
     redirect_to edit_org_person_path(current_user)
   else # Failed. Re-render the page as unsucessful
     render :edit
   end
 end

我的工厂:

factory :org_contact, :class => 'OrgContact' do |f|
   f.address1 "1324 Fake St"
   f.address2 "Unit 96B"
   f.city "Vancouver"
   f.typ_country_id "Canada" #country is an id of the corresponding record
   f.typ_region_id "British Columbia" #region is an id of the corresponding record
   f.postal_code "V5T 6C7"
   f.email "donn@example.com"
   f.business_number "1234433456"
   f.cell_number "1841355567"
end

当它们应该是相等的错误时,我得到的错误是“不相等的”。也就是说,

Failure/Error: specify { expect(user.reload.city).to  eq new_city }
   expected: "Toronto"
        got: "Vancouver"

   (compared using ==)

表单在 webapp 上手动运行。也就是说,如果我更新字段,它们会在数据库和所有内容中发生变化。但是,它只是在测试中不起作用。我感觉更新可能无法正常工作,但我不太确定如何调试它(对于 TDD、rails 和 rspec 来说仍然是新的)。

任何帮助将不胜感激!

【问题讨论】:

    标签: ruby-on-rails ruby rspec tdd


    【解决方案1】:

    您似乎将使用 org_person 的 id 而不是用户前往 edit_org_person_path。所以是的,在这种情况下,更新可能有效,但您正在使用错误的模型进行测试。

    乍一看,您似乎通过了前两个“地址”测试。但是,您的工厂地址与您填写字段时使用的地址相同,而您的测试地址也与此相同。

    一些想法:

    更改您的工厂以使用序列:

    factory :user do
      sequence( :email ){ |n| "user_#{n}@example.com" }
    end
    

    从那个工厂出来的模型每次都会收到一封新的电子邮件。

    你可以使用你的工厂来创建属性:

    let( :attributes ){ FactoryGirl.attributes_for( :org_contact) }
    

    这将允许:

    fill_in "Address 1", with: attributes[ :address1 ]
    

    您可能还对“撬轨”感兴趣: https://github.com/rweng/pry-rails

    在您的 gemfile 中(并记得打包),您可以执行以下操作:

    it 'lets me bind' do
      binding.pry
    end
    

    这将暂停测试并基本上将您置于当前环境的 irb 中。

    binding.pry 非常有用。您可以将它放在应用程序中的任何位置。请记住不要将其留在您的应用中。

    例如,我经常发现自己将其置于控制器的“创建”操作中:

    def create
      @thing = Thing.new( thing_params )
      binding.pry
    end
    

    当应用程序到达该代码时,我可以输入命令:

    > @thing.valid?
    false            ## hmmm
    > @thing.errors
     ....
    

    【讨论】:

    • 谢谢。但是,org_person_id 是指向 org_person 的 org_credential 和 org_contact 的 FK。如果记录存在,它在编辑功能中用于在我们的数据库中查找人员,并在需要时查找或创建他们的联系信息。谢谢你的撬轨。不知道存在。漂亮的宝石!
    猜你喜欢
    • 1970-01-01
    • 2014-11-22
    • 2018-04-22
    • 2011-09-25
    • 2011-06-24
    • 2011-08-27
    • 1970-01-01
    • 2023-03-03
    • 2016-07-22
    相关资源
    最近更新 更多