代码之家  ›  专栏  ›  技术社区  ›  Adam Berman

在Rails教程9.6.5中遇到问题,无法通过测试

  •  -3
  • Adam Berman  · 技术社区  · 10 年前

    已更新但仍不起作用!

    更新-我以为我已经包含了我正在使用的Rspec测试,但我没有。它们现在包括在内

    我做了建议的更改,但仍然没有通过。返回的失败测试给出的唯一提示是,它只是没有达到测试的目标。它似乎没有给出任何线索来解释为什么。。。

    按照清单9.49和9.50更改代码后,我很难通过测试。我感觉我在某个地方遗漏了一个细节,但我似乎找不到。下面是我的代码:

    编辑.html.erb 此页面应呈现共享的“字段”,该字段已作为练习的一部分进行了重构。

        <% provide(:title, "Edit user") %>
        <h1>Update your profile</h1>
    
        <div class="row">
            <div class="span6 offset3">
                <%= form_for(@user) do |f| %>
                    <%= render 'fields', f: f %>
                    <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
                <% end %>
    
                <%= gravatar_for @user %>
                <a href="http://gravatar.com/emails" target="_blank">change</a>
            </div>
        </div>
    

    新的.html.erb 此页面应呈现共享的“字段”,该字段已作为练习的一部分进行了重构。

        <% provide(:title, 'Sign up') %>
        <h1>Sign Up</h1>
    
        <div class="row">
            <div class="span6 offset3">
                <%= form_for(@user) do |f| %>
                    <%= render 'fields', f: f %>
                    <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
                <% end %>
            </div>
        </div>
    

    _字段.html.erb 此页面已被重构。我觉得错误与此有关,但我也不确定。

        <%= render 'shared/error_messages' %>
    
        <%= f.label :name %>
        <%= f.text_field :name %>
    
        <%= f.label :email %>
        <%= f.text_field :email %>
    
        <%= f.label :passsword %>
        <%= f.password_field :password %>
    
        <%= f.label :password_confirmation, "Confirm Password" %>
        <%= f.password_field :password_confirmation %>
    

    以下是不断失败的RSpec测试:

    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 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 "Confirm Password",      with: "foobar"
            end
    
            it "should create a 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_link('Sign out') }
                it { should have_title(user.name) }
                it { should have_selector('div.alert.alert-success', text: 'Welcome') }
            end
        end
    end
    
    describe "edit" do
        let(:user) { FactoryGirl.create(:user) }
        before do 
            sign_in user
            visit edit_user_path(user)
        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
    

    以下是我失败的测试:

    Failures:
    
    1) UserPages signup with valid information should create a user
       Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
       count should have been changed by 1, but was changed by 0
     # ./spec/requests/user_pages_spec.rb:85:in `block (4 levels) in <top (required)>'
    
    2) UserPages signup with valid information after saving the user 
       Failure/Error: it { should have_link('Sign out') }
       expected #has_link?("Sign out") to return true, got false
     # ./spec/requests/user_pages_spec.rb:92:in `block (5 levels) in <top (required)>'
    
    3) UserPages signup with valid information after saving the user 
       Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
       expected #has_selector?("div.alert.alert-success", {:text=>"Welcome"}) to return true, got false
     # ./spec/requests/user_pages_spec.rb:94:in `block (5 levels) in <top (required)>'
    
      4) UserPages signup with valid information after saving the user 
         Failure/Error: it { should have_title(user.name) }
         NoMethodError:
         undefined method `name' for nil:NilClass
         # ./spec/requests/user_pages_spec.rb:93:in `block (5 levels) in <top (required)>'
    
    >  5) UserPages edit with valid information 
    >     Failure/Error: it { should have_selector('div.alert.alert-success') }
    >       expected #has_selector?("div.alert.alert-success") to return true, got false
    >     # ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>'
    >
    >  6) UserPages edit with valid information 
    >     Failure/Error: specify { expect(user.reload.name).to eq new_name }
    >       
    >       expected: "New Name"
    >            got: "Person 65"
    >       
    >       (compared using ==)
    >     # ./spec/requests/user_pages_spec.rb:140:in `block (4 levels) in <top (required)>'
    >
    >  7) UserPages edit with valid information 
    >     Failure/Error: it { should have_title(new_name) }
    >       expected #has_title?("New Name") to return true, got false
    >     # ./spec/requests/user_pages_spec.rb:137:in `block (4 levels) in <top (required)>'
    >
    >  8) UserPages edit with valid information 
    >     Failure/Error: specify { expect(user.reload.email).to eq new_email }
    >       
    >       expected: "new@example.com"
    >            got: "person_64@example.com"
    >       
    >       (compared using ==)
    >     # ./spec/requests/user_pages_spec.rb:141:in `block (4 levels) in <top (required)>'
    >
    >Finished in 6.01 seconds
    >88 examples, 8 failures
    >
    >Failed examples:
    >
    >rspec ./spec/requests/user_pages_spec.rb:84 # UserPages signup with valid information should >create a user
    >rspec ./spec/requests/user_pages_spec.rb:93 # UserPages signup with valid information after >saving the user 
    >rspec ./spec/requests/user_pages_spec.rb:92 # UserPages signup with valid information after >saving the user 
    >rspec ./spec/requests/user_pages_spec.rb:94 # UserPages signup with valid information after >saving the user 
    >rspec ./spec/requests/user_pages_spec.rb:138 # UserPages edit with valid information 
    >rspec ./spec/requests/user_pages_spec.rb:140 # UserPages edit with valid information 
    >rspec ./spec/requests/user_pages_spec.rb:137 # UserPages edit with valid information 
    >rspec ./spec/requests/user_pages_spec.rb:141 # UserPages edit with valid information 
    >
    >Randomized with seed 6990
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   David Runger    10 年前

    看看你的_fields.html.erb

    改变 <%= f.label :passsword %> <%= f.label :password %> (强调“密码”中的S数。)