This is my feature spec test for creating a new post
require 'rails_helper'
require 'ffaker'
RSpec.feature 'Creating posts', :type => :feature do
scenario 'can create a new post' do
visit root_path
click_link 'New Post'
attach_file('Image', FFaker::File.file_name('spec/files', 'foo', 'jpg'))
fill_in 'Caption', with: 'Hello World! This is the first post!'
click_button 'Create Post'
expect(page).to have_content('Post was successfully created')
end
end
When I run the rspec command to run the test I get the following error
Failure/Error: attach_file('Image', FFaker::File.file_name('spec/files', 'foo', 'jpg'))
NameError:
uninitialized constant FFaker::File
How can I fix this?
Basically I htought to use ffaker for trying to upload an image instead of using a real image file. Is this supposed to work with ffaker, right?
The error tells you that Rails can't find FFaker::File module. FFaker gem reference tells me that there's actually no FFaker::File module in FFaker.
But there is one in Faker (Faker::File). And since FFaker is just refactored Faker I believe you can use Faker for your needs.
Related
I'm currently going through the Ruby on Rails Tutorial by Michael Hartl, and in Section 3.2.2 of the tutorial, he talks about using RSpec to test for the titles of the page. My tests (which I wrote myself, following his tutorial) kept failing the title tests (it can find the tag, but the title is always a blank string), and some searching on Google told me I needed to include the function render_views to pass the tests. My problem is that no matter what I try, RSpec returns a NoMethodError for render_views. The things I've tried:
config.render_views in spec_helper.rb
describe "Static Pages" do
render_views
...
end in the spec file
describe "Static Pages" do
RSpec::Rails::ViewRendering.render_views
...
end in the spec file
All of them return a NoMethodError.
I'm not following his Gemfile exactly, so the versions of Ruby and the relevant gems are:
Ruby: 1.9.3-p125
rails: 3.2.9
rspec: 2.12.0
rspec-rails: 2.12.0
capycabra: 2.0.1
I'm working in RubyMine 4.5.4, on Windows 7. Running the test in a command prompt returns the same errors however. The spec file is static_pages_spec.rb and is located in spec/requests
The code in my static_pages_spec.rb:
require_relative '../spec_helper'
describe "Static Pages" do
#RSpec::Rails::ViewRendering.render_views, returns NoMethodError
#render_views, returns NameError
describe "Home Page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => "Sample App")
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help Page" do
it "should have the h1 'Help'" do
visit '/static_pages/help'
page.should have_selector('h1', :text => "Help")
end
it "should have the title 'Help'" do
visit '/static_pages/help'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About Page" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => "About Us")
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About Us")
end
end
end
My spec_helper.rb is the spec_helper.rb automatically generated by creating running rails generate integration_test after creating the project with --no-test-framework, with these lines added in the config block:
# Fix NoMethodError for method 'visit'
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
EDIT
After some testing, I've managed to get the tests to pass by changing the capybara version to 1.1.2, while still using rspec 2.12.0 and rspec-rails 2.12.0 and not using render_views. While I'm glad the tests are passing, it still doesn't solve the problem of the missing render_views method, which still crops up if I try to use it.
Well, after a bit more testing, the mystery of the missing render_views method is solved. To include the render views method, apparently config.include RSpec::Rails::ViewRendering in the spec_helper is needed before calling config.render_views in the spec_helper or render_views in the spec file.
Secondly, with regards to the tests passing or failing, the problem seems to be in Capybara itself. Apparently, it seems that the way the visit method is implemented in 1.1.2 and 2.0.1 is significantly different in what it returns; even after including render_views, the test still fails (without using config.include Capybara::RSpecMatchers). Using it seems to provide an insight into its implementation though; the failure message from Capybara on using the RSpecMatchers is that it is looking for CSS instead of HTML. I don't claim to fully understand why it fails, however, so if anyone can enlighten me as to why Capybara 2.0.1 causes the tests to fail while 1.1.2 allows the tests to pass, that would be great.
Capybara 2.0 requires all tests to be in spec/features, and not in spec/requests.
You can read all about it here.
I would suggest that you stick to the versions mentioned in Hartl's tutorial, atleast in your beginner days. Will help you get through the tutorial faster!
I am trying to test my rails app with rspec 2.10.0 + capybara 1.1.2. Here is my test file
require 'spec_helper'
describe AdminPanelController do
describe "index" do
it "should have return code 200" do
visit '/admin'
page.should have_content "hello"
#response.status.should be(200)
end
end
end
And here are test result
Failure/Error: page.should have_content "hello"
Capybara::ElementNotFound:
Unable to find xpath "/html"
I google about this issue but find only information that webrat can be a problem however i do not have this gem installed. Thanks for any suggestions.
Wrong type of test. This looks like a controller test, which does tests with methods like get and post and is in the spec/controllers folder. Request specs, which use capybara, reside in spec/requests. Run $ rails generate scaffold SomeModel to see how they each should look.
If you understood the above but would still like to use capybara for your controller test, modify your describe block:
describe AdminPanelController, :type => :request do
...
end
I'm having problems making Capybara work with Rails. Just testing that suposedly interesting test thing. OK, in the attached code there are a couple of equivalent tests. The first one is made with shoulda-context + Test::Unit that comes with Rails. The second test is made with capybara and shoulda-context too.
require 'integration_test_helper'
class UsersTest < ActionDispatch::IntegrationTest
fixtures :all
context "signup" do
context "failure" do
setup do
#attr = { :name => "", :email => "", :password => "", :password_confirmation => "" }
end
should "not make a new user" do
assert_no_difference 'User.count' do
post_via_redirect "users", :user =>#attr # enviem les dades d'un nou usuari via create (POST /users)
assert_template 'users/new' # ens retorna a users/new, que significa que no s'ha creat l'usuari
assert_select "div#error_explanation" # comprovem que conte missatges d'error
end
end
should "not make a new user (capybara)" do
assert_no_difference 'User.count' do
visit '/signup'
fill_in 'Name', :with => #attr[:name]
fill_in 'Email', :with => #attr[:email]
fill_in 'Password', :with => #attr[:password]
fill_in 'Confirmation', :with => #attr[:password_confirmation]
click_button 'Sign Up!'
assert_template 'users/new' # ens retorna a users/new, que significa que no s'ha creat l'usuari
assert_select "div#error_explanation" # comprovem que conte missatges d'error
end
end
end
end
While the first one works OK, the capybara one throws this error message:
================================================================================
Error:
test: signup failure should not make a new user (capybara). (UsersTest):
ArgumentError: #request must be an ActionDispatch::Request
test/integration/users_test.rb:30:in `block (4 levels) in <class:UsersTest>'
test/integration/users_test.rb:23:in `block (3 levels) in <class:UsersTest>'
================================================================================
the required *integration_test_helper.rb* file is an accumulator of all suposed solutions I've found googling around and that don't work for me.
require 'test_helper'
require 'capybara/rails'
require 'database_cleaner'
# Transactional fixtures do not work with Selenium tests, because Capybara
# uses a separate server thread, which the transactions would be hidden
# from. We hence use DatabaseCleaner to truncate our test database.
DatabaseCleaner.strategy = :truncation
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# Stop ActiveRecord from wrapping tests in transactions
self.use_transactional_fixtures = false
teardown do
DatabaseCleaner.clean # Truncate the database
Capybara.reset_sessions! # Forget the (simulated) browser state
Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
end
end
Has anybody a solution? Should I try another Integration frmawork such as webrat?
My setup is:
marcel#pua:~/Desenvolupament/Rails3Examples/ror_tutorial$ rake about
About your application's environment
Ruby version 1.9.2 (x86_64-linux)
RubyGems version 1.8.15
Rack version 1.3
Rails version 3.1.3
JavaScript Runtime therubyracer (V8)
Active Record version 3.1.3
Action Pack version 3.1.3
Active Resource version 3.1.3
Action Mailer version 3.1.3
Active Support version 3.1.3
Middleware ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000002b9bac0>, Rack::Runtime, Rack::MethodOverride, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::RemoteIp, Rack::Sendfile, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
Application root /mnt/dropbox/Dropbox/DESENVOLUPAMENT/Rails3Examples/ror_tutorial
Environment development
Database adapter sqlite3
Database schema version 20120127011330
Also
shoulda-context (1.0.0)
capybara (1.1.2)
Thanks
You're mixing up your test types and attempting to assert a template in the wrong type of test. You should only be asserting templates within your Functional tests, where you're just directly testing a controller and not actually simulating a user interaction.
Capybara is meant specifically for Integration testing, which is essentially running tests from the viewpoint of an end-user interacting with a browser. In these tests, you should not be asserting templates because an end-user can't see that deep into your application. What you should instead be testing is that an action lands you on the correct path.
current_path.should == new_user_path
page.should have_selector('div#error_explanation')
See "The DSL" section in Capybara's README on git: https://github.com/jnicklas/capybara
Official explanation for your issue: https://github.com/jnicklas/capybara/issues/240
For my completeness, because i'll know that I'll come back to this link over and over:
Those using test unit and capybara this is a good primer too: from techiferous.
Notice the use of assert page.has_content?("something")
This is good to use as well as assert_equal some_path, current_path for testing routes.
Is it the most complete, don't know, but you don't need much more.
Thanks for your tips #Ryan. I was trying to figure out how to translate some RSpec Integration test examples from http://ruby.railstutorial.org/chapters/sign-up#sec:rspec_integration_tests into Test::Unit + Capybara. The original RSpec Integration test was
it "should not make a new user" do
lambda do
visit signup_path
fill_in "Name", :with => ""
fill_in "Email", :with => ""
fill_in "Password", :with => ""
fill_in "Confirmation", :with => ""
click_button
response.should render_template('users/new')
response.should have_selector("div#error_explanation")
end.should_not change(User, :count)
end
end
So, after your answer, I supose that the original example should not contain response.should render_template('users/new')
I'm setting up some integration tests using capybara and rspec.
In a single test, this works:
describe "SIGN IN, POST post" do
it "redirects to /posts after creating a new post" do
visit new_artist_session_path
fill_in 'Email', :with => 'vargas#vargas.com'
fill_in 'Password', :with => 'password'
click_link_or_button 'artist_submit'
visit "/artists/vargas/posts"
page.should have_content("Upload")
click_button 'Upload'
URI.parse(current_url).path.should == "/artists/vargas/posts"
end
end
However, I want to move the "sign_in" portion to a before(:all) filter block so that I can DRY up my tests. However it seems that within the before(:all) block, the same code gives this error:
Failure/Error: visit new_artist_session_path
NameError:
undefined local variable or method `new_artist_session_path' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010399e388>
It seems that the routes url helpers are not available from within the before block? How do I remedy this?
it appears that the paths helpers are available only between the "it" and "do". Just not in the before(:all) method. So I ended up just creating a method for signing in and included it in each test that needed it.
I had a bunch of combined controller/view tests written with rspec. I added the Capybara gem and wrote some integrations tests which pass fine. The only problem is that now in all my controller tests, where I have
response.should have_selector("some selector")
rspec gives errors such as:
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa03e7ec>
when I run controller tests. I'm guessing that Capybara is being used in my controller tests and has overwritten some Rspec methods. How can I fix this?
# gemfile.rb
group :test do
gem 'rspec'
gem "capybara"
gem "launchy"
gem 'factory_girl_rails', '1.0'
end
# spec_helper.rb
RSpec.configure do |config|
config.include IntegrationSpecHelper, :type => :request
end
Here's an example of a failing test:
# spec/controllers/books_controller_spec.rb
require 'spec_helper'
describe BooksController do
render_views
it "should have the right page title" do
get :show, :id => #book.ean
response.should have_selector("title", :content => "Lexicase | " + #book.title)
end
end
and it's associated error:
1) BooksController GET 'show' should have the right page title
Failure/Error: response.should have_selector("title", :content => "Lexicase | " + #book.title)
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa8488c0>
# ./spec/controllers/books_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
You were probably using Webrat earlier, and has_selector? is a Webrat matcher. Capybaras doesn't have a has_selector matcher, it has a matcher called has_css. You may want to replace the "has_selector" with "has_css".
Capybara helpers only works within requests specs. Either create a new request spec, or pass in :type => :request in the describe block part, like so:
describe "test for the testing test", :type => :request do
it "should work with capybara" do
visit root_path
click_link "Home"
page.should WHATEVA
end
end
I realize this question was asked a long time ago, but I thought I would share anyway. GLHF