Rspec错误,无法解决

时间:2014-09-12 20:12:39

标签: ruby-on-rails rspec

所以我正在测试公开的个人资料,我收到以下错误

1) Visiting profiles not signed in shows profile
 Failure/Error: expect( page ).to have_content(@post.title)
   expected to find text "Post title" in "Bloccit About Sign In or Sign Up Posts 
   Comments"
 # ./spec/features/profiles_spec.rb:22:in `block (3 levels) in <top (required)>'

这是我的profile_spec.rb

require 'rails_helper'

 describe "Visiting profiles" do 

  include TestFactories

  before do 
    @user = authenticated_user
    @post = associated_post(user: @user)
    @comment = Comment.new(user: @user, body: "A Comment")
    allow(@comment).to receive(:send_favorite_emails)
    @comment.save 
  end

  describe "not signed in" do 

    it "shows profile" do 
    visit user_path(@user)
    expect(current_path).to eq(user_path(@user))

    expect( page ).to have_content(@user.name)
    expect( page ).to have_content(@post.title)
    expect( page ).to have_content(@comment.body)
  end

 end
end

我不确定还有什么可能是错的。

看起来该视图正在查看主页面,而不是用户个人资料。

这是user_controller show

def show
  @user = User.find(params[:id])
  @posts = @user.posts
  @comments = @user.comments
end

1 个答案:

答案 0 :(得分:1)

mckay74 - 问题很轻微。

在ERB中,<% some ruby %>执行Ruby。 <%= some ruby %>执行该ruby并将其打印到页面。你使用了前者,所以当你的Ruby正在执行时,它不会出现在最终的HTML中。添加那些=,(到应该打印的行),你的测试应该通过。