If you test it on your browser, it will succeed, but if you test it on capybara, it will fail. If you submit it on the edit screen as shown below, update it.
def update
# When the post button is pressed, set published to true.If not pressed, save the draft automatically.
published_torf=params[:commit]?true:false
if published_torf
@ranking.update(ranking_params)
[email protected] lid?
@ranking.published=true
@ranking.save
end
response_with(@ranking)
else
@ranking.attributes=ranking_params
@ranking.save validate: false
end
end
I made the capybara work as follows:
require 'rails_helper'
#Device Test Helper
includeWarden::Test::Helpers
Warden.test_mode!
describe 'login process' do
let(:user) {create(:user)}
let(:category) {create(:category)}
describe 'post/edit' do
before(:each)do
category
login_as(user,:scope=>:user)
end
scenario 'Post/Edit Successful' do
visit root_path
click_button 'Post'
fill_in 'Ranking title', with: 'It's a title'
find('#ranking_ranking_posts_attributes_0_title').set('Article Title 3')
find('#ranking_ranking_posts_attributes_1_title').set('Article Title 2')
find('#ranking_ranking_posts_attributes_2_title').set('Article Title 1')
all('#edit').first.set('hey')
all('#edit')[1].set('hey')
all('#edit')[2].set('hey')
# When I click the post button, for some reason, the editing article that existed in the database disappears.
click_button 'Post'
expect(page).to have_content 'Title' #=> Error Occurred
end
end
end
The following error occurred:
Failure/Error: expect(page).to have_content 'This is the title'
expected to find text"Title: "in" RankingHook 100P New Arrival Order Bootflat PORTFOLIO Web Design Branding & Identity Mobile Design Print User Interface ABOUT The Company History Vision GALLERY Flickr PICASA iStockPhoto PhotoDune CONTACT Basic Infrastructure.Flight. 2014" All Flights
If you check with pry, the article disappears from the database when you press the send button.
By the way, in order to add the automatic draft saving function, when a new record is created, I try to save it and skip it to the edit screen as shown below.
def new
@ranking=Ranking.new
# Make this number the currentuser number
@ranking.posts_sum.times{
@ranking.ranking_posts.build
}
@ranking.user=current_user
@ranking.save validate: false
redirect_to edit_ranking_path@ranking
end
and
default
end
I'm sorry for the confusion, but I appreciate your cooperation.
---add ---
Below is the form.
<%=form_for(@ranking)do|f|%>
<div class="row category_select">
<div class="field col-sm-12">
<div class="col-sm-4">
<%=f.label:category_id%>
<br of >
<%=f.collection_select(:category_id,@categories,:id,:name,{}, {class:'form-control'})%>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="edit-rankingtop row">
<div class="field col-sm-2col-xs-12">
<strong id="top_image_label">top image</strong><br/>
<%[email protected]%>
<%[email protected], size: '65', id: 'top_image_thumb'%>
<%end%>
<div class="field">
<%=f.file_field: image%>
<br>
<%=f.hidden_field: image_cache%>
</div>
</div>
<div class="field col-sm-10 col-xs-12">
<%=f.label: title%>
<br>
<%=f.text_area: title, class: 'form-control', placeholder: 'Stuff up to 150 characters' %>
</div>
<br of >
<div class="field">
<%=f.hidden_field:user_id, :value=>current_user.id%>
</div>
<div class="clearfix"></div>
</div>
<!--Notification Location for Auto-Draft Save -->
<%[email protected]==true%>
<div class="create-temp col-sm-12 col-xs-12">/div>
<%end%>
<br of >
<%[email protected]_sum%>
<div class="col-sm-10col-xs-12row">
<div class="field col-sm-12">
<%=f.fields_for —ranking_posts, @ranking.ranking_posts do | p | %>
<br of >
<h4>%=i%>Location</h4>
<%=p.hidden_field:rank, :value=>i%>
<div class="field">
<%=p.label: title%>
<%=p.text_field: title, class: 'form-control'%>
</div>
<div class="field">
<%=p.label:description%>
<%=p.text_area:description, class: 'form-control', id: 'edit'%>
</div>
<%i=i-1%>
<%end%>
</div>
<div class="actions col-sm-6 col-xs-12">
<%=f.submit 'post',:class=>'btn btn-success btn-block'%>
</div>
<%end%>
Also, I will list the Ranking and RankingPost models.
class Ranking <ActiveRecord::Base
belongs_to —user
belongs_to —category
has_many:ranking_posts,dependent::destroy
accepts_nested_attributes_for —ranking_posts
mount_uploader: image, ImageUploader
# order
default_scope->{order('created_at DESC')}
values:title, :presence=>true, length: {maximum:150}
values:category_id,:presence=>true
Please complete validates_associated:ranking_posts, message:'.'
end
and
class RankingPost<ActiveRecord::Base
belongs_to —Ranking
default_scope->{order('rank DESC')}
values:title,:description,:rank, presence:true
end
That's right. Nice to meet you.
ruby ruby-on-rails rspec capybara
The reason was that some div elements were not closed in the form, so capybara treated the elements in the form as toplevel elements.Therefore, the data entered in the form was not sent by submit to the server, and the DB record was updated with empty data.
When entered from a web browser, the process was successful because the browser handled the undisturbed div elements appropriately, and capybara handles HTML strictly.
これ This is likely to be a place to fall in love with
Also, after correcting the div element to close properly, the test now passes.
© 2024 OneMinuteCode. All rights reserved.