I'm trying to execute this test:
def test_create
get :new
assert_template "admin/supplier/new"
assert_difference 'Supplier.count' do
post :create, :supplier => {:name => 'Juan', :province => 'provincia'}
assert_response :redirect
assert_redirected_to :action => 'index'
end
assert_equal 'Supplier Juan was succesfully created.', flash[:notice]
end
The following line throws an error:
assert_template "admin/supplier/new"
Here's the error:
test_create(Admin::SupplierControllerTest) [supplier_controller_test.rb:25]:
expecting <"admin/supplier/new"> but rendering with <"">
but <"admin/supplier/new"> doesn't work.
with some version, Rails stopped evaluating templates out of its "functional" tests.
You need to tell Rails to run the templates for this test. Either write a "view" test (which come with their own issues!).
The fix for RSpec is to add #render_views to your tests. I used the equivalent command for non-RSpec tests during my last project, so naturally I can't remember what it was now!
Related
I'm using Rails 5 and minutest and trying to write a test for a controller. The test is
# issues_controller_test.rb
class IssuesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "logged in should get issues page" do
sign_in users(:one)
test_stop_id = 1
test_line_id = 1
get new_issue, :stop_id => test_stop_id, :line_id => test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
end
The method in question attempts to access this page, defined in my rails routes ...
new_issue GET /issues/new(.:format) issues#new
Yet when I run the test, I get this error
# Running:
.E
Error:
IssuesControllerTest#test_logged_in_should_get_issues_page:
NameError: undefined local variable or method `new_issue' for #<IssuesControllerTest:0x007f816759b330>
test/controllers/issues_controller_test.rb:10:in `block in <class:IssuesControllerTest>'
bin/rails test test/controllers/issues_controller_test.rb:6
Finished in 0.103956s, 19.2389 runs/s, 9.6195 assertions/s.
Why is the test framework unable to find a method defined in my routes file?
new_issue that way is just a local variable defined nowhere, try specifying it as a working url with new_issue_url:
test 'logged in should get issues page' do
sign_in users :one
test_stop_id = 1
test_line_id = 1
get new_issue_url, stop_id: test_stop_id, line_id: test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
I am trying to test my Grape API, but I am receiving a 400 error in my tests, but when I run the action the test is supposed to test, I get a 201 HTTP response as expected. Not sure what is going on here. Below is the specific RSpec test, but you can view the whole project with the factories and the actual Grape API on GitHub at hackcentral/hackcentral. The test below is testing the POST create action on Alpha::Applications. (app/api/alpha/applications.rb)
describe 'POST #create' do
before :each do
#oauth_application = FactoryGirl.build(:oauth_application)
#token = Doorkeeper::AccessToken.create!(:application_id => #oauth_application.id, :resource_owner_id => user.id)
end
context "with valid attributes" do
it "creates a new application" do
expect{
post "http://api.vcap.me:3000/v1/applications?access_token=#{#token.token}", application: FactoryGirl.attributes_for(:application), :format => :json
} .to change(Application, :count).by(1)
end
it "creates a new application, making sure response is #201" do
post "http://api.vcap.me:3000/v1/applications", application: FactoryGirl.attributes_for(:application), :format => :json, :access_token => #token.token
response.status.should eq(201)
end
end
end
I don't understand why are you testing http://api.vcap.me an not localhost?
You usually test the app on the local enviroment. And this is not the right why to test if the server is working either.
Here is an example of how your test should look like.
https://github.com/dblock/grape-on-rails/blob/master/spec/api/ping_spec.rb from an example project
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).
I have 2 simple RSpec tests i've written for a small rails app i've done to learn rails. I originally had a mock setup for my Link class but was getting the same issue.
This is my test code:
require 'spec_helper'
describe LinksController do
render_views
before(:each) do
link = Link.new
link.stub!(:title).and_return("Reddit")
link.stub!(:url).and_return("http://www.reddit.com")
link.stub!(:created_at).and_return(Time.now)
link.stub!(:updated_at).and_return(Time.now)
link.stub!(:user_id).and_return("1")
link.stub!(:id).and_return("1")
link.save
user = User.new
user.save
end
it "renders the index view" do
get :index
response.should render_template('links/index')
response.should render_template('shared/_nav')
response.should render_template('layouts/application')
end
it "renders the show view" do
get :show, :id => 1
response.should render_template('links/show')
response.should render_template('shared/_nav')
response.should render_template('layouts/application')
end
end
I'm new to both rails and RSpec, not sure what I should be doing to get this to work. What is the best way to test this show method from my LinksController when you need data? I tried mock_model too but maybe I was using it wrong.
You can see all the app code on Github
The problem is that you are stubbing a model, so it's not stored in the database. That means that when you call get :show, :id => 1 the query to the database returns nothing and your tests fail. Stubbing is great when you want to fake a response or an object without using the database, but if you are depending on actual Rails code that uses the database you can't use this method because nothing exists in the database. To fix this I would drop the stubbed models entirely and actually create them.
require 'spec_helper'
describe LinksController do
render_views
before(:each) do
user = User.create
#link = Link.create :title => "Reddit",
:url => "http://www.reddit.com",
:user => user
end
it "renders the index view" do
get :index
response.should render_template('links/index')
response.should render_template('shared/_nav')
response.should render_template('layouts/application')
end
it "renders the show view" do
get :show, :id => #link.id
response.should render_template('links/show')
response.should render_template('shared/_nav')
response.should render_template('layouts/application')
end
end
You should also eventually look into Factory gems like Sham and Factory Girl to simplify the creation of test data.
I have some troubles with testing in Rails 3. I'm currently upgrading a Rails2 app to Rails3. I'm using shoulda for testing. In my functional tests, I'm testing with shoulda, that a GET should respond with success
context "GET to :blame" do
should "mark a song as blamed" do
get :blame, :id => #song.id
assert_equal Blame.count, 1
get :blame, :id => #song.id
assert_equal Blame.count, 2
end
should respond_with :success
end
In the next to last line I'm getting the following error while executing the functional tests with rake test:functionals:
1) Error:
test: a visitor GET to :blame should respond with 200. (SongsControllerTest):
NoMethodError: undefined method `response_code' for nil:NilClass
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:57:in `response_code'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:48:in `correct_status_code?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:30:in `matches?'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/assertions.rb:53:in `assert_accepts'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:324:in `block in should'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'
I'm using Ruby 1.9.2, Rails 3.0.3 and Shoulda 2.11.3.
Hope, someone can help me.
thx,
tux
The two should blocks are run separately and no request would be made for respond_with hence the error. You would need to make the request in a setup block such as:
context "GET to :blame" do
setup do
get :blame, :id => #song.id
end
should "mark a song as blamed" do
assert_equal Blame.count, 1
end
should respond_with :success
end