Expected response to be a <:success>, but was <301> - ruby-on-rails

I have application without tests, now I'm trying to add them (minitest-rails), but something wrong.
I'm typing rails generate controller test test
and I have:
require "minitest_helper"
class TestControllerTest < MiniTest::Rails::ActionController::TestCase
test "should get test" do
get :test
assert_response :success
end
end
now I'm typing rake test and Expected response to be a <success>, but was <301> error occuring.
Where is the problem?
Edited:
My controller's code:
class TestController < ApplicationController
def test
end
end

I've solved it! Just deleted force_ssl and added config.force_ssl = true in production config.

Related

Rails 5 get users_path(10) test doesn't fail even no "show" method defined in controller

Me gain, yes. Got a weird problem. This test should fail but it isn't.
Using Rails 5 --api only.
Got a UsersController like so:
class UsersController < ApplicationController
def index
users = User.all
render json: users
end
end
Test file:
require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
test "users should return list of users" do
get users_path
assert_response :success
assert_not response.body.empty?
end
test "show valid id should return user json" do
get users_path(10)
assert_response :success
end
end
Routes.rb defined like this:
Rails.application.routes.draw do
resources :cats
resources :users
end
The console output:
Running via Spring preloader in process 4176
Run options: --seed 28710
# Running:
........
Finished in 0.100764s, 79.3937 runs/s, 138.9390 assertions/s.
8 runs, 14 assertions, 0 failures, 0 errors, 0 skips
Why is the test not reporting error when the controller clearly has no show method defined ?
Going to my browser localhost:3000/users/10 returns JSON saying "show" method does not exists.
Any ideas?
Replace users_path(10) in the test with user_path(10)

functionality testing of controller in ruby on rails

I have product controller in that i have method say homepage and _homepage.html.erb file in views. How to test this method to get assert should get response success. I created productcontrllertest.rb in test/functional and wrote below code
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
test "should get homepage" do
get :homepage
assert_response :success
end
end
and i have routes.rb file with get "product/homepage"
when i run test with command bundle exec rake test:functionals i am getting error.
Error is that NoMethodError: undefined method `authenticate' for nil:NilClass
Please help me to fix this error.
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
test "should get homepage" do
get 'homepage/1'
assert_response :success
end
end

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

functional test for Rails plugin, assert_template broken?

I'm writing a plugin that adds a class method to ActionController::Base, so in my functional test I am creating a simple controller to test against. The example below is totally stripped down to basically do nothing. The test succeeds all the way up to assert_template, which fails.
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; end
end
ActionController::Routing::Routes.draw {|map| map.resources :under_test }
class MyPluginTest < ActionController::TestCase
def setup
#controller = UnderTestController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
test "should render the correct template" do
get :index
assert_template :index # everything works except this assert
end
end
Here are the results from running the above file:
Loaded suite /Users/jjulian/sandbox/vendor/plugins/my_plugin/test/my_plugin_test
Started
E
Finished in 0.014567 seconds.
1) Error:
test_should_render_the_correct_template(MyPluginTest):
NoMethodError: undefined method `[]' for nil:NilClass
method assert_template in response_assertions.rb at line 103
method clean_backtrace in test_case.rb at line 114
method assert_template in response_assertions.rb at line 100
method test_should_render_the_correct_template in so_test.rb at line 22
method __send__ in setup_and_teardown.rb at line 62
method run in setup_and_teardown.rb at line 62
1 tests, 0 assertions, 0 failures, 1 errors
My questions: what am I missing to allow assert_template to work? Am I requiring the correct libraries? Extending the correct TestCase class?
I figured it out. First, the missing template is because the view_path needs to be explicitly set in the controller under test:
before_filter {|c| c.prepend_view_path(File.join(File.dirname(__FILE__), 'fixtures')) }
I created a dummy erb file in test/fixtures/under_test/index.erb. That brings me back to the nil error in response_assertions.rb. Well, it looks like I need to include another test class:
require 'action_view/test_case'
Now it works.
One additional note: assert_template uses match to determine success, so in this case, assert_template 'ind' would succeed, as would assert_template 'x'.
Try replacing the setup method in the test with just tests UnderTestController. Rails takes care of setting up the test controller, request and response for you automatically.

Resources