使用Rails Tutorial 9.6.5时遇到问题,无法通过测试

时间:2014-09-04 00:35:09

标签: ruby-on-rails rspec railstutorial.org

已更新,但仍然无效!

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

我提出了建议的更改,但它仍然没有通过。返回失败测试给出的唯一提示是它没有达到测试的目标。它似乎没有提供任何关于为什么......

的线索

按照清单9.49和9.50更改代码后,我无法通过测试。我有一种感觉,我在某个地方错过了一个细节,但我似乎无法找到它。这是我的代码:

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

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

_fields.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 个答案:

答案 0 :(得分:2)

看看你的_fields.html.erb

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