Hartl Rails教程(3.2)8.2.5 Rspec失败

时间:2012-04-17 20:48:00

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

更新:

我的测试没有提交用户信息。相关代码在前一章的练习中:

  describe "after saving user" do
    before { click_button submit }
    let(:user) { User.find_by_email('user@example.com') }
    it { should have_selector('title', text: user.name) }
    it { should have_selector('div.alert.alert-success', text: 'Welcome') }
    it { should have_link('Profile') }
  end

/ UPDATE

我已经完成了第8.2.5节(登录时登录),该应用程序的行为完全如下所述:

  • 用户在注册时登录
  • 然后重定向到他们的个人资料页面
  • 标题已更改为包含“退出”链接。

但我对'退出'链接的测试失败了。这是我的代码,全部都是从教程中复制的:

相关控制器代码(users_controller.rb):

def create
  @user = User.new(params[:user])
  if @user.save
    sign_in @user
    flash[:success] = "Welcome to the Sample App!"
    redirect_to @user
  else
    render 'new'
  end
end

相关视图代码(_header.html.erb):

<ul class="dropdown-menu">
  <li><%= link_to "Profile", current_user %></li>
  <li><%= link_to "Settings", '#' %></li>
  <li class="divider"></li>
  <li>
    <%= link_to "Sign out", signout_path, method: "delete" %>
  </li>
</ul>

相关测试代码(user_pages_spec.rb):

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
  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 user" do
      expect { click_button submit }.to change(User, :count).by(1)
    end

    describe "after saving user" do
      it { should have_link('Profile') }
    end
  end
end

错误为rspec ./spec/requests/user_pages_spec.rb:47 # User pages signup with valid information after saving user

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为最后一个'describe'块应该是这样的:

  describe "after saving user" do
    before { click_button submit }
    it { should have_content('Profile') }
  end

在检查页面上是否有适当的内容之前,测试错过了点击“提交”按钮。