简体   繁体   English

ruby on rails教程错误第9章

[英]ruby on rails tutorial errors chapter 9

I have been working on the ruby on rails tutorial. 我一直在研究ruby on rails教程。 I am totally new to this stuff. 我对这些东西全新。 I am in chapter 9 and totally stuck. 我在第9章并完全陷入困境。 I am hoping someone might help me decipher the error messages. 我希望有人可以帮我解密错误信息。 Up until now I have just searched them and figured it out but I think it would be more valuable to just learn how to decipher the error message to fix the problem. 到目前为止,我刚刚搜索过它们并将其弄清楚,但我认为了解如何破译错误消息以解决问题会更有价值。 If it is too long or cumbersome to explain than than I totally understand if no one wants to take it on. 如果解释的时间太长或太麻烦,而不是我完全理解,如果没有人想要接受它。 I was unable to find something online that helped me with this on my own. 我无法找到在线帮助我自己的东西。

Here are the errors that I am currently getting 以下是我目前遇到的错误

1) AuthenticationPages signin with valid information authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page 1)在登录后尝试访问受保护页面时,AuthenticationPages使用有效信息授权登录非登录用户应该呈现所需的受保护页面

 Failure/Error: click_button "Sign in"
 Capybara::ElementNotFound:
   Unable to find button "Sign in"
 # ./spec/requests/authentication_pages_spec.rb:61:in `block (7 levels) in <top (required)>'

2) AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller visiting the edit page 2)AuthenticationPages在访问编辑页面的Users控制器中为非登录用户签署有效信息授权

 Failure/Error: it { should have_title('Sign in') }
   expected #has_title?("Sign in") to return true, got false
 # ./spec/requests/authentication_pages_spec.rb:76:in `block (8 levels) in <top (required)>'

3) AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller as wrong user visiting Users#edit page 3)AuthenticationPages使用有效的信息授权登录用户控制器中的非登录用户作为错误的用户访问用户#edit页面

 Failure/Error: before { sign_in user, no_capybara: true }
 NoMethodError:
   undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2::Nested_2::Nested_2::Nested_1::Nested_2::Nested_3::Nested_1:0xabbb9e4>
 # ./spec/requests/authentication_pages_spec.rb:87:in `block (8 levels) in <top (required)>'

4) AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller as wrong user submitting a PATCH request to the User#update action 4)AuthenticationPages使用有效的信息授权为Users控制器中的非登录用户签名,因为错误的用户向User#update action提交PATCH请求

 Failure/Error: before { sign_in user, no_capybara: true }
 NoMethodError:
   undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2::Nested_2::Nested_2::Nested_1::Nested_2::Nested_3::Nested_2:0xa82d354>
 # ./spec/requests/authentication_pages_spec.rb:87:in `block (8 levels) in <top (required)>'

Finished in 6.6 seconds 65 examples, 4 failures 完成6.6秒65例,4次失败

Failed examples: 失败的例子:

rspec ./spec/requests/authentication_pages_spec.rb:66 # AuthenticationPages signin with valid information authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page rspec ./spec/requests/authentication_pages_spec.rb:66#AuthenticationPages在登录后尝试访问受保护页面时,对非登录用户的有效信息授权签名,应该呈现所需的受保护页面

rspec ./spec/requests/authentication_pages_spec.rb:76 # AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller visiting the edit page rspec ./spec/requests/authentication_pages_spec.rb:76#AuthenticationPages使用访问编辑页面的Users控制器中的非登录用户的有效信息授权登录

rspec ./spec/requests/authentication_pages_spec.rb:91 # AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller as wrong user visiting Users#edit page rspec ./spec/requests/authentication_pages_spec.rb:91#AuthenticationPages使用有效信息授权登录Users控制器中的非登录用户,因为错误的用户访问用户#edit页面

rspec ./spec/requests/authentication_pages_spec.rb:96 # AuthenticationPages signin with valid information authorization for non-signed-in users in the Users controller as wrong user submitting a PATCH request to the User#update action rspec ./spec/requests/authentication_pages_spec.rb:96#AuthenticationPages使用有效信息授权登录Users控制器中的非登录用户,因为错误的用户向User#update action提交PATCH请求

authentication_pages_spec.rb authentication_pages_spec.rb

require 'spec_helper'

describe "AuthenticationPages" do


subject { page }


describe "signin page" do
    before { visit signin_path }

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

describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
        before { click_button "Sign in" }

        it { should have_title('Sign in') }
        it { should have_selector('div.alert.alert-error', text: 'Invalid') }

        describe "after visiting another page" do
            before { click_link "Home" }
            it { should_not have_selector('div.alert.alert-error', text: 'Invalid') }
        end
    end

    describe "with valid information" do
        let(:user) { FactoryGirl.create(:user) }

        before do
            fill_in "Email",     with: user.email.upcase
            fill_in "Password",  with: user.password
            click_button "Sign in"
        end

        it { should have_title(user.name) }
        it { should have_link('Profile',     href: user_path(user)) }
        it { should have_link('Settings',    href: edit_user_path(user)) }
        it { should have_link('Sign out',    href: signout_path) }
        it { should_not have_link('Sign in', href: signin_path) }

        describe "followed by signout" do
            before { click_link "Sign out" }
            it { should have_link('Sign in') }
        end

        describe "authorization" do

            describe "for non-signed-in users" do
                let(:user) { FactoryGirl.create(:user) }

                describe "when attempting to visit a protected page" do
                    before do
                        visit edit_user_path(user)
                        fill_in "Email",    with: user.email
                        fill_in "Password", with: user.password
                        click_button "Sign in"
                    end

                    describe "after signing in" do

                        it "should render the desired protected page" do
                            expect(page).to have_title('Edit user')
                        end
                    end
                end

                describe "in the Users controller" do

                    describe "visiting the edit page" do
                        before { visit edit_user_path(user) }
                        it { should have_title('Sign in') }
                    end

                    describe "submitting to the update action" do
                        before { patch user_path(user) }
                        specify { expect(response).to redirect_to(signin_path) }
                    end

                    describe "as wrong user" do
                        let(:user) { FactoryGirl.create(:user) }
                        let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
                        before { sign_in user, no_capybara: true }

                        describe "visiting Users#edit page" do
                            before { visit edit_user_path(wrong_user) }
                            it { should_not have_title(full_title('Edit user')) }
                        end

                        describe "submitting a PATCH request to the User#update action" do
                            before { patch user_path(wrong_user) }
                            specify { expect(response).to redirect_to(root_url) }
                        end
                    end
                end
            end
        end
    end
  end
 end

user_pages_spec.rb user_pages_spec.rb

      require 'spec_helper'

 describe "User pages" do

subject { page }

describe "signup page" do
before { visit signup_path }

it { should have_content('Sign up') }
 it { should have_title(full_title('Sign up')) }
end

describe "signup" do

before { visit signup_path }

let(:submit) { "Create my account" }

describe "with invalid information" do
  it "should not create a user" do
    expect { click_button submit }.not_to change(User, :count)
  end

  describe "after submission" do
    before { click_button submit }
    it { should have_title('Sign up') }
    it { should have_content('error') }
  end


  describe "with valid information" do
    before do
      fill_in "Name",  with: "Example User"
      fill_in "Email",with: "user@example.com"
      fill_in "Password",  with: "foobar"
      fill_in "Confirmation", with: "foobar"
    end

    it "should create a new user" do
      expect { click_button submit }.to change(User, :count).by(1)
    end

    describe "after saving the user" do
      before { click_button submit }
      let(:user) { User.find_by(email: 'user@example.com') }

      it { should have_title(user.name) }
      it { should have_selector('div.alert.alert-success', text: 'Welcome') }
    end

    it "should create a user" do
      expect { click_button submit }.to change(User, :count).by(1)
    end

    describe "edit" do
      let(:user) {FactoryGirl.create(:user) }
      before do
        sign_in user
        visit edit_user_path(user)
      end

      describe "page" do
        it { should have_content("Update your profile") }
        it { should have_title("Edit user") }
        it { should have_link('change', href: 'http://gravatar.com/emails') }
      end

      describe "with invalid information" do
        before { click_button "Save changes" }

        it { should have_content('error') }
      end

      describe "with valid information" do
        let(:new_name) { "New Name" }
        let(:new_email) { "new@example.com" }
        before do
          fill_in "Name",             with: new_name
          fill_in "Email",            with: new_email
          fill_in "Password",         with: user.password
          fill_in "Confirm Password", with: user.password
          click_button "Save changes"
        end

        it { should have_title(new_name) }
        it { should have_selector('div.alert.alert-success') }
        it { should have_link('Sign out', href: signout_path) }
        specify { expect(user.reload.name).to eq new_name }
        specify { expect(user.reload.email).to eq new_email }
      end
    end
  end
     end
  end
 end

I am particularly curious what "in 'block(7 levels)in" means. 我特别好奇“在'块(7级)中”是什么意思。 Are the levels something on that page or is it referring to something else? 这个级别上的某些内容是某些内容还是指其他内容? What is 'The block' in reference to? 什么是'块'参考?

I also cannot seem to understand the capybara error that I am getting. 我似乎也无法理解我得到的水豚错误。 It appeared in chapter 8, went away and now has come back. 它出现在第8章,走了,现在又回来了。 The button is there on the page when I bring it up so capybara is just not finding it I guess. 当我把它拿出来的时候,页面上有按钮,所以我认为水豚只是找不到它。 Can anyone explain how that works? 任何人都可以解释这是如何工作

What does the 'nested" refer to? '嵌套'是指什么?

Anyone know a good website that breaks this down? 谁知道一个好的网站打破了这个? I would be more than happy to do the work myself but I could not find one. 我会非常乐意自己做这项工作,但我找不到。 I would really love to be able to decipher this myself instead of just googling it and hoping the answer is somewhere or having to rely on someone else to explain it all the time. 我真的很想能够自己解读这个,而不仅仅是谷歌搜索它,希望答案在某个地方,或者不得不依靠别人来解释它。

Thanks so much for your time and any help. 非常感谢您的时间和任何帮助。

Now is the time on Stack Overflow when I oversimplify. 现在是Stack Overflow的时候我过度简化了。 (Google 'ruby block' to read a lot more on this.) A block in Ruby is a bunch of code that gets passed to a method like an argument. (Google的'ruby block'可以在这里阅读更多内容。)Ruby中的一个块是一堆代码,它被传递给像参数这样的方法。 For example, 例如,

[1,2,3].each{|n| puts n * n }

each is the method (called on the array [1,2,3]), and everything in the brackets is the block. each是方法(在数组[1,2,3]上调用),括号中的所有内容都是块。 The way the method each works is, it takes every element in the enumerable it`s called on ([1,2,3]) and yields one element at a time to the code block: each方法的工作方式是,它会调用枚举中的每个元素([1,2,3])并一次生成一个元素到代码块:

|the first element is 1| puts 1 * 1
|next is 2| puts 2 * 2
|etc| puts 3 * 3

A block can also be written between do...end . 也可以在do...end之间写入一个块。 The Ruby way is to use brackets if you can fit the block on one line, and do...end otherwise - which is just how you have it in your specs. Ruby的方法是使用括号,如果你可以将块放在一行上,并且do...end否则 - 这就是你在规范中的方式。 Everywhere you have a do and a matching end in your specs is a block, nested one inside another. 不论你有do和匹配end在你的规格是块,一个套在另一个内部。 The methods are harder to notice, because RSpec makes it look like natural language, but every time you write describe or it at the start of a line is a method call. 这些方法很难被注意到,因为RSpec使它看起来像自然语言,但每次你在一行的开头写一个describeit是一个方法调用。 (So are let and before and subject and expect , for that matter, which get called with single line blocks in your specs.) (所以被letbeforesubject ,并expect ,对于这个问题,它被调用与您的规格一行块)。

So the message 'block(7 levels)' means your error is nested in that many blocks: 所以消息'block(7 levels)'意味着你的错误嵌套在那么多块中:

describe "AuthenticationPages" do #starts the first block
  ...
  describe "signup" do #starts the second

and so on. 等等。

Now, your error messages. 现在,您的错误消息。 The first and second are basically telling you the same thing - you visit edit_user_path(user) and you don't see a "Sign In" button or "Sign In" in the page title. 第一个和第二个基本上是告诉你同样的事情 - 你访问edit_user_path(user) ,你没有在页面标题中看到“登录”按钮或“登录”。 Check the log/test.log file - what happens when you visit that page? 检查log / test.log文件 - 访问该页面时会发生什么? Is it a redirect to the signin page? 它是重定向到登录页面吗? It ought to be, but it looks like it isn't. 它应该是,但它看起来不是。

The other two error messages say exactly the same thing - the spec doesn't know what sign_in means. 另外两个错误消息说完全相同 - 规范不知道sign_in含义。 You need to have a method by that name defined somewhere RSpec can find it - either in the spec itself, or the spec_helper file that you require at the top of the spec, or in some file which is itself require d inside spec_helper. 你需要在RSpec可以找到它的地方定义一个方法 - 在规范本身,或在规范顶部require的spec_helper文件,或者在spec_helper内部本身require某个文件中。

Finally, I think Hartl is right - you Google as best you can with error messages and stack traces, ask when you can't find what you're looking for, and you'll get better figuring things out yourself with time. 最后,我认为Hartl是对的 - 你最好用谷歌的错误信息和堆栈痕迹,当你找不到你想要的东西时问你,你会随着时间的推移自己弄清楚。

Re: sign_in -- The sign_in function was added to spec/support/utilities.rb in section 9.1.1, Listing 9.6 (in Rails 4 version of book). Re:sign_in - 在第9.1.1节,清单9.6(在Rails 4版本的书中)中将sign_in函数添加到spec / support / utilities.rb中。

I got the same error because my function in utilities.rb was "signin" without the underscore. 我得到了同样的错误,因为我在utilities.rb中的函数是“signin”而没有下划线。 Once I added the underscore (and changed the other reference to the same function to match), the test went green. 一旦我添加了下划线(并将另一个引用更改为匹配的相同函数),测试变为绿色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM