Hartl的铁路教程列出了8.4 rspec测试失败的奇怪之处

时间:2022-04-06 19:23:21

I'm working through Michael Hartl's railstutorial book and I've come to a strange behavior that I can't seem to troubleshoot. In the listing 8.4 it says the rspec tasks should be passing, however I'm getting a failure, with the error being:

我正在研究Michael Hartl的铁路教程书,我遇到了一个奇怪的行为,我似乎无法排除故障。在清单8.4中它说rspec任务应该通过,但是我遇到了失败,错误是:

Failures:

  1) Authentication signin page 
     Failure/Error: before { visit signin_path }
     AbstractController::ActionNotFound:
       The action 'sign_in' could not be found for StaticPagesController
     # ./spec/requests/authentication_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) Authentication signin page 
     Failure/Error: before { visit signin_path }
     AbstractController::ActionNotFound:
       The action 'sign_in' could not be found for StaticPagesController
     # ./spec/requests/authentication_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.11364 seconds
2 examples, 2 failures

I don't understand why it's looking for 'sign_in' path instead of 'signin' which I have in my routes:

我不明白为什么它在寻找'sign_in'路径而不是我在路线中的'signin':

  match '/signup',  to: 'users#new',             via: 'get'
  match '/signin',  to: 'sessions#new',          via: 'get'
  match '/signout', to: 'sessions#destroy',      via: 'delete'

The rspec test also doesn't ask for 'sing_in'... it uses 'signin_path' in the test:

rspec测试也没有要求'sing_in'...它在测试中使用'signin_path':

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_content('Sign in') }
    it { should have_title('Sign in') }
  end
end

Is rails or rspec automatically transcribing signin to sign_in? And also why is it looking for it in the StaticPagesController instead of the SessionsController? - I guess this is because it's looking for /sign_in instead of /signin? So it sounds like rspec is changing it to sign_in?

rails或rspec是否会自动将signin转录为sign_in?还有为什么它在StaticPagesController而不是SessionsController中寻找呢? - 我想这是因为它正在寻找/ sign_in而不是/ signin?所以听起来rspec正在改变它为sign_in?

1 个解决方案

#1


0  

well, I think I figured out the issue. In Michael's routes.rb file, he has:

好吧,我想我弄明白了这个问题。在Michael的routes.rb文件中,他有:

root 'static_pages#home'

However, when I changed it to:

但是,当我将其更改为:

root to: 'static_pages#home'

All the tests pass now. It makes sense that root was defining the static_pages controller as the default controller to look for paths. by adding 'to:' it just matches the '/' path to static_pages#home. Maybe I'm wrong, but that's what it looks like to me so far.

现在所有测试都通过了。 root用户将static_pages控制器定义为查找路径的默认控制器是有道理的。通过添加'to:',它只匹配static_pages #home的'/'路径。也许我错了,但到目前为止,这就是我的样子。

#1


0  

well, I think I figured out the issue. In Michael's routes.rb file, he has:

好吧,我想我弄明白了这个问题。在Michael的routes.rb文件中,他有:

root 'static_pages#home'

However, when I changed it to:

但是,当我将其更改为:

root to: 'static_pages#home'

All the tests pass now. It makes sense that root was defining the static_pages controller as the default controller to look for paths. by adding 'to:' it just matches the '/' path to static_pages#home. Maybe I'm wrong, but that's what it looks like to me so far.

现在所有测试都通过了。 root用户将static_pages控制器定义为查找路径的默认控制器是有道理的。通过添加'to:',它只匹配static_pages #home的'/'路径。也许我错了,但到目前为止,这就是我的样子。