Run Environment
rails 7.0.3
ruby 3.1.2
rspec 3.11
When you use the browser, you can see that it transitions to the path you expected, but the RSpec system test does not match the path you expected, and the test fails.
If the name is blank in a simple form test that simply registers the author's name, an error message will appear and I do not know why the test to render the new registration screen will fail.
routes.rb
resources:authors
app/model/author.rb
validates:name, presence:true
app/controller/authors_controller
def new
@author=Author.new
end
def create
@author=Author.new(author_params)
[email protected]
redirect_to authors_path, notice: "Registered #{@a-uthor.name}"
else
render:new, status::unprocessable_entity
end
end
private
def author_params
param.require(:author).permit(:name)
end
app/views/authors/new.html.haml
=simple_form_for @author do|f|
= f.input:name
= f.button:submit
spec/system/authors_spec.rb
context 'when required field name is not entered' do
it 'cannot create' do
visit new_author_path
click_button 'Register'
expect(page).to have_content 'Please enter author name'
expect(page).to have_current_path new_author_path
end
end
The results of the above author_spec.rb
run will fail because you expect the author's list screen.
1) Failure/Error: expect(page).to have_current_path new_author_path expected"/authors "to equal"/authors/new"
that cannot be created when the author's new required registration name is not entered.
I'm not very experienced, but I was curious, so I wrote it down.
Is it a form of user login and registration?
From where no user creation, login, etc. are listed,
I thought it was transitioning to the list screen because I didn't log in.
I'm sorry if I said something wrong.
When you use a browser, you can confirm that you want to transition to the path you want.
verifying:
Does this mean that the URL path of the screen where "Enter Author Name" is displayed is /author/new
? The contents of the screen displayed are the contents of the /authors/new template, but is the URL path /authors
?
Description:
Typically, resources:authors
in routes.rb associates the create
action with post/authors
.And the create
action does not redirect_to
but render:new
if @author.save
is falsy
.Because post/authors
displays the authors/new template, the URL path should not change from /authors
.
Also, this URL path is the same as the list = index
action = get/authors
, which was actually reported as an actual value in the test.
When you use a browser, you can confirm that you want to transition to the path you want.
Again, when the error occurs, is the path /authors
*1? I think the content displayed is *2 from the author/new template.
11 create
URL path
except HTTP method for action post/authors
22 render:new
Results
Supplemental:
Therefore, I think the following two points will be expected as a test.
Does this mean that the URL path of the screen where "Please enter the author's name" is /authors/new?
That's right.
The contents of the screen displayed are the contents of the /authors/new template, but the URL path is /authors?
If the author fails to create, an error message is displayed, and the URL path when the error occurs is also /author/new.
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.