When I created a new registration form in rails and wrote a test in RSpec that failed when the name was blank, I wrote a test in have_current_path that I expected a new registration screen as a transition destination after the failure, but somehow I expected a list screen and the test failed.

Asked 1 years ago, Updated 1 years ago, 346 views

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.

Implementation

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.

ruby-on-rails ruby

2022-10-19 00:00

3 Answers

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.


2022-10-19 00:00

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 createURL 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.

  • The URL path must be authros_path
  • The contents of the page must be a new registration screen form


2022-10-19 00:00

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.


2022-10-19 00:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.