【问题标题】:Having trouble with Rails Tutorial 9.6.5, can't get tests to passRails Tutorial 9.6.5 遇到问题,无法通过测试
【发布时间】:2014-09-04 00:35:09
【问题描述】:

已更新,但仍然无法使用!

更新 - 我以为我已经包含了我正在使用的 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

【问题讨论】:

  • Confirmation != Confirm Password.
  • 更新了建议的更改,但仍然没有通过。返回的失败测试给出的唯一提示是它没有达到测试的目标。它似乎没有提供任何关于原因的线索。

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


【解决方案1】:

看看你的 _fields.html.erb

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

【讨论】:

  • 谢谢!我知道这是很小的事情,但我想不通。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
相关资源
最近更新 更多