I am setting up automatic testing for my Rails application. I am working on my controller test and I am getting an unrecognized id error. I know the edit route works for the app right now because I can go to the actual link(/adjusters/3293/edit), but I cannot get my test to past.
def setup
#adjuster = Adjuster.new(adjuster_name: "TestLastName", address_1: "4511 W 200 S", id: 1)
end
test "should get edit" do
get edit_adjuster_path(#adjuster)
assert_response :success
end
When I rake my routes the edit route shows.
edit_adjuster GET /adjusters/:id/edit(.:format) adjusters#edit
Here is the error I am receiving
Couldn't find Adjuster with 'id'=1
My show route works though
test "should get show" do
get adjusters_path(#adjuster)
assert_response :success
end
Related
This is my test for the chapters controller
require 'test_helper'
class ChaptersControllerTest < ActionController::TestCase
test "should get index_nklm" do
get :index_nklm
assert_response :success
end
test "should get show_nklm" do
get show_chapter_nklm_path(chapters(:one))
assert_response :success
end
end
This is my routes.rb
get 'chapters_nklm' => 'chapters#index_nklm', as: :chapters_nklm
get 'chapter_nklm/:id/show' => 'chapters#show_nklm', as: :show_chapter_nklm
This is my chapters.yml fixture:
one:
id: 12
chapter_name: MyString
chapter: MyString
description: MyText
display: 1
subject_id: 1
This is (part of) the chapters_controller.rb:
def show_nklm
#chapter = Chapter.find(params[:id])
end
And I don't know why I get this error message?
Error:
ChaptersControllerTest#test_should_get_show_nklm:
ActionController::UrlGenerationError: No route matches {:action=>"/chapter_nklm/12/show", :controller=>"chapters"}
test/controllers/chapters_controller_test.rb:11:in `block in <class:ChaptersControllerTest>'
But when I copy "/chapter_nklm/12/show" into the browser, everything works fine? Where is my mistake and which concept didn't I understand?
Thanks in advance!
get is waiting to receive an action as a symbol object, you're passing the whole route alias, plus the object needed.
Try using the params option with your route:
test 'should get show_nklm' do
get :show_nklm, params: { id: chapters(:one).id }
assert_response :success
end
Perhaps you should consider doing something like:
resources :chapters do
get :nklm, to: :show_nklm
end
Which would give you:
chapter_nklm GET /chapters/:chapter_id/nklm(.:format) chapters#show_nklm
chapters GET /chapters(.:format) chapters#index
POST /chapters(.:format) chapters#create
new_chapter GET /chapters/new(.:format) chapters#new
edit_chapter GET /chapters/:id/edit(.:format) chapters#edit
chapter GET /chapters/:id(.:format) chapters#show
PATCH /chapters/:id(.:format) chapters#update
PUT /chapters/:id(.:format) chapters#update
DELETE /chapters/:id(.:format) chapters#destroy
This strikes me as a bit more conventional, and should allow you to do:
class ChaptersControllerTest < ActionController::TestCase
...
test "should get show_nklm" do
get chapter_nklm_path(chapters(:one))
assert_response :success
end
end
I'm completing the exercise to add a Contact page, but the testing fails on the page title.
Here is my testing file:
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
#base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get root" do
get root_url
assert_response :success
end
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | #{#base_title}"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | #{#base_title}"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | #{#base_title}"
end
test "should get contact" do
get static_pages_about_url
assert_response :success
assert_select "title", "Contact | #{#base_title}"
end
end
Here is the contact.html.erb file:
<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
Contact the Ruby on Rails Tutorial about the sample app at the
contact page.
</p>
I've also completed the following:
Added the appropriate route
Added the appropriate action
However I get this error message:
test_should_get_contact#StaticPagesControllerTest (0.45s)
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<About | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.
test/controllers/static_pages_controller_test.rb:35:in `block in <class:StaticPagesControllerTest>'
Please also note that
The page displays correctly, with the expected page title (Contact not About)
I tested again using a completely new page, but had the same result with 'About' being returned in page title
Really not sure why it's returning this as I've followed Tutorial closely. I want to progress in Tutorial, but if I cannot resolve this basic testing issue, I'm not sure I'll get very far!
Please check your code on the second line of this code block.
test "should get contact" do
# get static_pages_about_url # This is wrong correct it to as below
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | #{#base_title}"
end
You have given a test case to check the contact page title on the about url which obviously will fail the test.
You should be testing for contact page title on the contact url like above.
Make the change and you should get going!
Also a word of motivation, just keep going even if things don't make sense right now cause later they will. Cheers :)
I think you might try to replace the line get static_pages_about_url (under test "should get contact" do) with:
get static_pages_contact_url
What happens is that your test is calling the wrong url (about, instead of contact), causing the error when checking the <title>.
I was running tests without a problem on a cloud9 console. I made what I thought was a basically inconsequential change in my code to fix a testing fail, and got this error message:
rake aborted!
NameError: undefined local variable or method `migrateRails' for main:Object
The change I made was just to add a function to a controller, nothing to do with the test gem. I reinstalled the bundle and ran the test again. Same error.
I undid the change in the controller. Same error.
Thanks.
I think I find my error: when I wrote the test I didn't put "end" at the end of the test.
I did:
test "should get contact" do
get :contact
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
and I should have done:
test "should get contact" do
get :contact
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
I've been trying to use FactoryGirl for tests on my Rails application, but I'm running into difficulty with it.
I feel as if there must be something fairly obvious I'm doing wrong, but after much searching I haven't been able to figure out the cause.
I'm trying to run a test to confirm the 'show' action is successful on one of my controllers.
Here's the error message I'm getting:
Failure/Error: get 'show'
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"simple_requests"}
Below are the relevant code snippets leading to this outcome.
/spec/controllers/simple_requests_controller_spec.rb
require 'spec_helper'
describe SimpleRequestsController do
describe "GET 'show'" do
before do
#simple_request = build(:simple_request)
end
it "should be successful" do
get 'show'
expect(response).to be_success
end
end
end
/factories/simple_requests_controller_spec.rb
FactoryGirl.define do
factory :simple_request do
id 123
full_name "Testie McTesterson"
company "Test Company"
role "Analyst"
email "foobar#foobs.com"
phone "000888"
message "Test question?"
end
end
/controllers/simple_requests_controller.rb
def show
authorize SimpleRequest #For pundit
#simple_request = SimpleRequest.find(params[:id])
end
I have two hypotheses as to why this may be happening:
1) Rspec is looking for an id for the 'show' action, but somehow can't find it. (Although there is one in the Factory, and I've yet to figure out how it wouldn't be flowing through.)
2) Pundit is causing issues, since the show action may require authorization (although commenting out the 'authorize' line makes no difference at present)
Any and all thoughts welcome :)
EDIT
Pasting below the output of rake routes | grep simple_requests
simple_requests GET /simple_requests(.:format) simple_requests#index
POST /simple_requests(.:format) simple_requests#create
new_simple_request GET /simple_requests/new(.:format) simple_requests#new
edit_simple_request GET /simple_requests/:id/edit(.:format) simple_requests#edit
simple_request GET /simple_requests/:id(.:format) simple_requests#show
PATCH /simple_requests/:id(.:format) simple_requests#update
PUT /simple_requests/:id(.:format) simple_requests#update
DELETE /simple_requests/:id(.:format) simple_requests#destroy
Edit 2 - Adding ID parameter
I have now also attempted to add an id as follows:
it "should be successful" do
get 'show', id: #simple_request.id
expect(response).to be_success
end
This time I received the following error message
ActiveRecord::RecordNotFound: Couldn't find SimpleRequest with 'id'=123
'123' is the ID in my /factories - I think I must be missing something to get this working, but can't figure out what yet.
Your SimpleRequest does not have an Id. You need to use create instead of build
before do
#simple_request = create(:simple_request)
end
it "should be successful" do
get 'show', id: #simple_request.id
expect(response).to be_success
end
Try this:
before do
#simple_request = create :simple_request
end
it "should be successful" do
get 'show', id: #simple_request.id
expect(response).to be_success
end
It's a show view, so you'll have to supply your request with an ID and you'll have to actually create a record with create instead of build in your before block.
I am trying to troubleshoot a problem that I am having with my test database. I am currently following alongside Agile Web Development 4 and going through the chapters. Somewhere along 3/4ths of the way I discovered that my test database was persisting data and am currently trying to find out where exactly this is taking place. I have an orders_controller_test.rb file that I am trying to run.
When I execute
rake test:controllers
rake test test/controllers/orders_controller_test.rb
rake test
the tests execute and the data is persisted. I want to go test by test one at a time and am executing this command to do so:
rake test test/controllers/orders_controller_test.rb name_of_test
as shown in the Rails 4 guide. The execution of this command yields this:
As you can see it executes the tests but doesn't actually do so. No assertions are made and this is confusing/frustrating me. I've referred to this web article and have tried the methods in it. Different ways to run rails tests
Why is this not executing correctly. This is immensely frustrating and I suspect it is something simple. Can someone please help pinpoint what is possibly going on? Help would be greatly appreciated.
-----EDIT-----------
Controller tests
require 'test_helper'
class OrdersControllerTest < ActionController::TestCase
def setup
#order = orders(:one)
end
def teardown
#order = nil
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:orders)
end
test "should get new" do
get :new
assert_response :redirect
end
test "should create order" do
assert_difference('Order.count') do
post :create, order: { address: #order.address, email: #order.email, name: #order.name, pay_type: #order.pay_type }
end
assert_redirected_to store_path
end
test "should show order" do
get :show, id: #order
assert_response :success
end
test "should get edit" do
get :edit, id: #order
assert_response :success
end
test "should update order" do
patch :update, id: #order, order: { address: #order.address, email: #order.email, name: #order.name, pay_type: #order.pay_type }
assert_redirected_to order_path(assigns(:order))
end
test "should destroy order" do
assert_difference('Order.count', -1) do
delete :destroy, id: #order
end
assert_redirected_to orders_path
end
test "requires item in cart" do
get :new
assert_redirected_to store_path
assert_equal flash[:notice], 'Your cart is empty'
end
test "should get new order" do
item = LineItem.new
item.build_cart
item.product = products(:ruby)
item.save!
binding.pry
session[:cart_id] = item.cart.id
get :new
binding.pry
assert_response :success
end
end
It appears that your test method name is incorrect. I believe you have to prepend test_ to the method name.
rake test test/controllers/orders_controller_test.rb -n test_should_get_new_order
# or
rake test test/controllers/orders_controller_test.rb - test_should_get_new_order
Rails converts test 'some name' do to test_some_name to run the test(s).