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 8182987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:01:27+00:00 2026-06-07T01:01:27+00:00

I am new to capybara & rspec and i wrote a simple test(under spec/requests)

  • 0

I am new to capybara & rspec and i wrote a simple test(under spec/requests) to test the my root path:

# encoding: utf-8
require 'spec_helper'

describe "select a course" do
  before { visit root_path }

  it "should render main page well" do
    puts page.html
    page.should have_xpath("//ul[@class='thumbnails']/li[1]")
  end
end

The root page contains both static and dynamic content which indeed contains the above xpath statement by firefinder verification. But the test failed. The reason was that after “visit root_path”, the result(page.html) only contained the static part of the whole root. I don’t know why.

I then try standalone capybara without rails & rspec and it worked correctly.
The spec_helper.rb:

    # This file is copied to spec/ when you run 'rails generate rspec:install'     
ENV["RAILS_ENV"] ||= 'test'                                                    
require File.expand_path("../../config/environment", __FILE__)                 
require 'rspec/rails'                                                          
require 'rspec/autorun'                                                        
require 'capybara/rspec'                                                       
require 'capybara/rails'                                                       

# Requires supporting ruby files with custom matchers and macros, etc,         
# in spec/support/ and its subdirectories.                                     
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}              

RSpec.configure do |config|                                                    
  # ## Mock Framework                                                          
  #                                                                            
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #                                                                            
  # config.mock_with :mocha                                                    
  # config.mock_with :flexmock                                                 
  # config.mock_with :rr                                                       

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
  config.fixture_path = "#{::Rails.root}/spec/fixtures"                        

  # If you're not using ActiveRecord, or you'd prefer not to run each of your  
  # examples within a transaction, remove the following line or assign false   
  # instead of true.                                                           
  config.use_transactional_fixtures = true                                     

  # If true, the base class of anonymous controllers will be inferred          
  # automatically. This will be the default behavior in future versions of     
  # rspec-rails.                                                               
  config.infer_base_class_for_anonymous_controllers = false                    
end  

root page:

<div class="row">
  <%= render partial: 'shared/courses_category', object: @big_categories, as: 'courses_big_categories' %>
  <div class="span9 courses">
    <ul class="thumbnails"> 
      <% @courses.each do |course| %>
        <%= render(partial: 'shared/course', object: course) %>
      <% end %>
    </ul>
  </div>
</div>

And it should looked like:

<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
  <a href="/courses/2"><img src="http://placehold.it/260x180" alt="掌握ruby"></a>
  <div class="caption">
    <h5>掌握ruby</h5>
    <p class="course-summary">够fashin够cool的动态语言,应用广泛,简洁直观,让你一生受用</p>
    <a class="btn btn-primary" href="/select_courses/buy/2">购买</a>&nbsp;
    <a class="btn" href="/select_courses/store/2">收藏</a>
    <span class="course-price">¥200</span>
  </div>
</div>

But the result(not including the header and footer) was as follows:

<div class="row">                           
  <div class="span3 courses-category-panel">
    <h2>课程分类</h2>                       
  </div>                                    

  **<div class="span9 courses">               
    <ul class="thumbnails"></ul>            
  </div>**                                    
</div> 

We could see that the following dynamic parts are NOT generated:

<%= render partial: 'shared/courses_category', object: @big_categories, as: 'courses_big_categories' %>

<% @courses.each do |course| %>
    <%= render(partial: 'shared/course', object: course) %>
<% end %>

Could anyone see this problem or help it?

Add some more info:
root_path is matched to Welcome#index which is defined as follows:

def index
    @big_categories = BigCategory.all
    @courses = Course.all
end
  • 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-07T01:01:29+00:00Added an answer on June 7, 2026 at 1:01 am

    Is this your entire testing file?

    # encoding: utf-8
    require 'spec_helper'
    
    describe "select a course" do
      before { visit root_path }
    
      it "should render main page well" do
        puts page.html
        page.should have_xpath("//ul[@class='thumbnails']/li[1]")
      end
    end
    

    It seems like your subject is not defined in the rspec file.

    You should have subject { page } probably after require 'spec_helper'.

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

Sidebar

Related Questions

Hi I have a very simple integration test require 'integration_test_helper' Capybara.current_driver = :rack_test class
I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
I'm writing an integration test for a rails application using Capybara within Rspec. After
I am new to capybara but I am trying to use it to test
I'm setting up some integration tests using capybara and rspec. In a single test,
My spec require 'spec_helper' describe 'user_sessions/new.html.erb' do let (:user_session) { mock_model(UserSession).as_null_object } before do
I'm using RSpec/Capybara as my test suite. I have some javascript that dynamically appends
I have a very simple RSpec Capybara have_selector() that doesn't seem to be working
I am new to Capybara. I try to use visit root_path and then check
new to c#. I'm trying to make a simple system where I can search

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.