【发布时间】: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