Hi there am working on a rails project, using a vagrant box(with ubuntu server fully configured with passenger, trying a hand at developing on a production like environment because this is my target production environment).
My problem though is that when i run "rails g controller people" and i write a simple controller spec like;
require 'spec_helper'
describe PeopleController do
describe "GET #index" do
it "responds with success" do
get :index
expect(response).to be_success
end
end
end
and i run it with "rspec spec/controllers/", the test passes right away even without having created an index action in the controller.
What do you think could be causing this?
Thank you
Is there an index.html.erb in app/views/people? If so Rails implicitly creates an action for you that renders the template.
Related
I am trying to make some functional tests against new functionality in an application that uses Spree.
The problem that I am running into is that when I launch my created test, the path variables that should exists are not there for the test to execute.
Say I have these routes in my local routes.rb:
Spree::Core::Engine.routes.draw do
namespace :admin do
resources :image_games
end
end
Now in my test I would have the following:
require 'test_helper'
module Spree
module Admin
class ImageGamesControllerTest < ActionController::TestCase
test 'Should get index' do
get admin_image_games_path
assert_response :success
end
end
end
end
What happens is that a Minitest::UnexpectedError: NameError: undefined local variable or methodadmin_image_games_path'` error will be thrown.
I found some advice about this but all of the solutions seem to be for RSpec. I would like to keep using Test::Unit as I feel more at home with it.
So my question, how to load in routes from a foreign Rails Engine into functional tests?
EDIT:
It seems that just the helpers are not working for my tests.
Using get '/store/admin/image_games' works but get admin_image_games_path does not. When running the application normally then these helpers do work tough.
The simple controller testing code works in a SQL backend rails application, but not in the mongodb backend rails application.
require 'test_helper'
class PostsController < ActiveController::TestCase
test "should get index" do
get :index
assert_response :success
end
end
While running rake --verbose test, no normal successful output and it exits & prints nothing.
Any clue what might be wrong?
I have figured out the root cause of the problem. When require "rails/test_unit/railtie" is added to config/application.rb, the test can be run without problem.
While following Michael Hartl's Rails tutorial, I was experimenting with some custom functions in my test section, and ran into a restriction that surprised me. Basically, global path variables (eg "root_path") only work within the "do...end" block of an "it" section within a "describe" block of the RSpec tests.
I believe the following details boil down to the question, what is special about the "it" block which enabled "root_path" to work there while not working outside of the "it" block?
(I've determined a workaround, but I'm curious whether there's a solid explanation of this behavior.)
File: spec/requests/static_pages_spec.rb
This fails:
require 'spec_helper'
def check_stable(path)
it "should be stable" do
get path
response.status.should be(200)
end
end
describe "StaticPages" do
describe "Home => GET" do
check_stable(root_path)
end
end
This succeeds:
require 'spec_helper'
describe "StaticPages" do
describe "Home => GET" do
it "should be stable" do
get root_path
response.status.should be(200)
end
end
end
The failure is basically:
$ bundle exec rspec spec/requests/static_pages_spec.rb
Exception encountered: #<NameError: undefined local variable or method `root_path' for #<Class:0x00000004cecd78>>
... any idea why?
I tried all of the suggestions on these two threads:
Hartl's Tutorial Section 5.3.2: Rails Routes
Rspec and named routes
None worked until I sussed out the issue above.
Yes, named routes work only within it or specify blocks. But it's easy to modify the code:
def should_be_stable(path)
get path
response.status.should be(200)
end
describe "StaticPages" do
describe "Home => GET" do
it { should_be_stable(root_path) }
end
end
You steel need to include url_helpers
it blocks (or specify blocks) are what denote actual tests. Inside a test, you will have access to the full complement of Rails and Rspec helpers; outside the test, not so much (as you have worked out).
Hello and thanks for you patience!
My rails app uses a combination of rspec and shoulda to run tests. The tests are automated over guard and spork. One of my controllers tests looks like
it {should respond_with(:success)}
When running tests i get
Expected response to be a 200, but was 301
manually testing by browsing & wget things go right, the page is responding correctly with 200 status code. As I am quite new to rails testing, proberly I am not understanding how the tests are currently ran. How are they implemented? What was the purpose of the environment 'test'? Is there some kind of webserver running in backgroud to run the tests? Obviously there is some kind of non-wanted redirecting.
Thanks in advance!
Edit: More sources
controller:
class PlansController < ApplicationController
def index
#plans=Plan.all
end
... more methods ...
end
test:
describe PlansController do
before :each do
#plan=FactoryGirl.create(:plan)
end
context " get :index" do
before do
get :index
end
it {should respond_with(:success)}
end
... more tests..
end
You are missing an :each in the before block of the context for get :index so you are never calling the index action.
Update as follows:
context " get :index" do
before(:each) do
get :index
end
it { should respond_with(:success) }
end
I'm currently new to RSpec and trying to implement some Controller testing with RSpec
In my Rails app, I'm using Devise as my authentication system. My question is, When we test a controller which uses some authentication system (in my case Devise), what is the standard practice?
Is it
1 - to skip the authentication
or
2 - to authenticate the controller
as per the question, following is my controller
require File.dirname(__FILE__) + '/../spec_helper'
describe ProjectsController do
include Devise::TestHelpers
p "starting..."
before(:each) do
p "in before method"
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in Factory.create(:user)
end
it "should create a project" do
p "should create a project"
end
after(:each) do
#user.destroy unless #user.nil?
end
end
I can only see 'starting', But why its not going to "in before method" and "should create a project"
I'm using Rspec2 and Rails2 on Ubuntu.
Check this: Stubbing Devise in rSpec and Rails3.
Standard practice is not skipping authentication, but effectively making sure that a correct user is logged in (for devise).
Referring to your code: have you tried to create some real test? E.g. something as simple as
it "gets index" do
get :index
response.status.should be == 200
end
I am not sure why you are not seeing the print-statements. Either rspec skips the empty step (there is no real code), or because something else went wrong. But honestly, I am not even sure if using p inside rspec works.
A tool like rubymine allows you to easily debug your specs if you want to step into it (which imho is a better approach then the scattered p statements).