How to test rails application with mongodb(mongoid) backend? - ruby-on-rails

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.

Related

Testing against engine routes in Rails with Test::Unit

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.

Rails minitest syntax for get :index

I am reading the Hartl tutorial and I have a question about whether or not this is the right syntax. How does rails know what controller to look in? I have 10 controllers and I am writing a test for static pages controller. How does it know what get :home refers to and where? Does it look in the associated controller with the file name? Or does it look in routes?
Also, is assert_response a rack test or a rails test?
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
get :help
assert_response :success
end
end
How does it know what get :home refers to and where?
Rails does a lot of magic, some of this magic falls into the category of convention over configuration.
In this case, the fact that the name of this test's class is StaticPagesControllerTest tells the test suite that it is testing the StaticPagesController.
It is standard rails convention to name tests this way. You could technically configure any test to look at any controller or action you want, but following this convention has many benefits (another discussion).
When you tell it to get :home it is using the get http method on the controller action named :home.
Also, is assert_response a rack test or a rails test?
It asking rails for the status code received. In this case it is checking to see if the status code was 200.
Additional info: http://api.rubyonrails.org/classes/ActionDispatch/Assertions/ResponseAssertions.html#method-i-assert_response

Rspec tests passing without actions being implemented

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.

shoulda, rspec, guard, spork issues with http responses

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

Test (with RSpec) a controller outside of a Rails environment

I'm creating a gem that will generate a controller for the Rails app that will use it. It's been a trial and error process for me when trying to test a controller. When testing models, it's been pretty easy, but when testing controllers, ActionController::TestUnit is not included (as described here). I've tried requiring it, and all similar sounding stuff in Rails but it hasn't worked.
What would I need to require in the spec_helper to get the test to work?
Thanks!
Here's an example of a working standalone Test::Unit test with a simple controller under test included.. Maybe there's some parts here that you need to transfer over to your rspec code.
require 'rubygems'
require 'test/unit'
require 'active_support'
require 'active_support/test_case'
require 'action_controller'
require 'action_controller/test_process'
class UnderTestController < ActionController::Base
def index
render :text => 'OK'
end
end
ActionController::Routing::Routes.draw {|map| map.resources :under_test }
class MyTest < ActionController::TestCase
def setup
#controller = UnderTestController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
test "should succeed" do
get :index
assert_response :success
end
end

Resources