Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:27:32+00:00 2026-06-15T15:27:32+00:00

I have a many-to-many association between a Post and a Tag model. Then, I

  • 0

I have a many-to-many association between a Post and a Tag model.
Then, I have the following in my post view:

posts/show.html.erb:

<p><%= raw @post.tags.map { |t| link_to t.name, tag_path(t.name) }.join(', ') %></p>

This is the spec I wrote:

post_pages_spec.rb:

  describe "show page" do
    let!(:post) { FactoryGirl.create(:post, user:        user,
                                            title:       "Lorem",
                                            content:     "Lorem ipsum",
                                            category_id: category.id,
                                            tag_list:     "#{tag.id}") }

    before do 
      sign_in user
      visit post_path(post)
    end

    it { should have_selector('h1',       text: post.title) }
    it { should have_selector('title',    text: post.title) }
    it { should have_link(post.user.name, href: user_path(user)) }
    it { should have_selector('p',        text: post.content) }
    it { should have_selector('p',        text: post.category.name) }
    it { find("p").should have_content(post.tags.map { |t| t.name }.join(", ")) }
    .
    .
    .

I’m getting this error:

Failures:

1) Post pages show page
Failure/Error: it { find(“p”).should have_content(post.tags.map { |t| t.name }.join(“, “)) }
expected there to be content “1, tag28” in “Lorem ipsum”
# ./spec/requests/post_pages_spec.rb:28:in `block (3 levels) in ‘

The code works in the real site, but as you can see, the spec is failing. What’s the right way of writing this spec?

EDIT:

An example of the post model (just in case):

class Post < ActiveRecord::Base
  include ActionView::Helpers

  attr_accessible :title, :content, :category_id, :tag_list

  has_many :taggings, :dependent => :destroy  
  has_many :tags, :through => :taggings
  .
  .
  .

Output from live site:

  <p><a href="/tags/inkscape">inkscape</a>, <a href="/tags/gimp">gimp</a></p>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:27:34+00:00Added an answer on June 15, 2026 at 3:27 pm

    Update

    From the comment thread, we’ve figured out that replacing the find with a simple have_selector passes:

    it { should have_selector('p', text: post.tags.map { |t| t.name }.join(", ")) }
    

    It’s not clear why find('p').should have_content(...) does not work. I’ve tested the same type of view (with <p> tags wrapping a list of links) and found that the find('p').should ... pattern works fine, so something funny is happening in your code. It may or may not be related to the issue with have_content in Capybara 2.0, which I discuss below.

    If anyone has any ideas please share them! This is the best I can offer.

    Original answer

    The new text matcher in Capybara 2.0 excludes content that is not visible, producing somewhat unintuitive results:

    it { should have_selector('title', text: 'Some Title') } # <= fails
    it { should have_selector('title') }                     # <= passes
    it { should have_text('Some Title') }                    # <= passes
    

    And, relevant to the case in this question, this:

    find("title").should have_content("some text") # <= fails
    

    See also this post: How can I test the page title with Capybara 2.0?

    If page.body.should ... is working but page.should ... (or in another form, subject { page} ; it { should ... }) isn’t, then perhaps this is the issue. Note that page.body is an HTML string, whereas page is a Capybara::Session, they’re totally different, although it seems in Capybara 2.0 at least you can run your expectations against either. I think page.body includes all HTML, hence bypassing the visibility issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-many association between a Post and a Category model: categorization.rb: class
I have Product model and it has many categories with a has_many :through association
I have the following model association: a student model and has_many scores. I need
I have the following :has_many, :through association for my models: Group Model: has_many :group_users
I have a many-to-many relationship defined between two entities using an association table and
I have a many-to-many association between 'Category' and 'Product' entities in my Entity Framework
I am using Entity Framework 4. I have a many-to-many association (relationship) between two
I have two entities, Account and Subscription with a many-to-many association between them. I
I have a has_many :through association setup between two tables (Post and Category). The
I have two models, subject and page . I created a one-to-many association between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.