StatementInvalid: SQLite3::BusyException: database is locked - ruby-on-rails

I have referred various answers on StackOverflow but I still haven't been able to resolve this issue. I am sharing the code for my controllers_test.rb and along with that the screenshot. Every time I run rails test, the following error is shown. I have made sure that console is close. In case there is any other source code needs to be share please do let me know.
require "test_helper"
class CategoriesControllerTest < ActionDispatch::IntegrationTest
setup do
#category = Category.create(name: "Sports")
end
test "should get index" do
get categories_url
assert_response :success
end
test "should get new" do
get new_category_url
assert_response :success
end
test "should create category" do
assert_difference('Category.count') do
post categories_url, params: { category: { name: "Travel" } }
end
assert_redirected_to category_url(Category.last)
end
test "should show category" do
get category_url(#category)
assert_response :success
end
end
test_helper.rb
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors, with: :threads)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end

Related

Why use setup method for test suites in Ruby on Rails?

I am working through railstutorial.org and in our first test suite we setup an instance variable in a setup method.
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
def setup
#base_title = "Ruby on Rails Tutorial Sample App"
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
end
Why do we do this instead of just defining it straight up as an instance variable?
You are defining it as an instance variable. When the test is run the class (StaticPagesControllerTest) is instantiated and the setup function called on that instance, not on the class. If you tried to do the following instead it would define a class level instance variable not accessible from instances:
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
#base_title = "Ruby on Rails Tutorial Sample App"
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Home | #{#base_title}" # XXX not defined on instance
end
...
See this page for one explanation of the difference.
You could use a class variable (##base_title) but that might cause surprising consequences later, e.g. if you wanted to subclass the test.
Here's a quick demonstration:
class First
#a = 'hoge'
##b = 'hoge'
def initialize
#c = 'hoge'
end
def give_me_a
#a
end
def give_me_b
##b
end
def give_me_c
#c
end
end
f = First.new
# => #<First:0x007fb81607fcd0>
f.give_me_a
# => nil
f.give_me_b
# => "hoge"
f.give_me_c
# => "hoge"

Why does Rails fail to run a single rake test?

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).

How to create a route for testing purposes?

I'm writing tests with rspec for my application controller in my rails app (written in Rails 4) and I'm running into a problem where it doesn't recognize the route for the HTTP request I'm sending. I know there's a way to do this using MyApp::Application.routes but I'm not able to get it working.
#application_controller_spec.rb
require 'spec_helper'
class TestController < ApplicationController
def index; end
end
describe TestController do
before(:each) do
#first_user = FactoryGirl.create(:user)
# this is to ensure that all before_filters are run
controller.stub(:first_time_user)
controller.stub(:current_user)
end
describe 'first_time_user' do
before(:each) do
controller.unstub(:first_time_user)
end
context 'is in db' do
before(:each) do
#user = FactoryGirl.create(:user)
controller.stub(:current_user).and_return(#user)
end
it 'should not redirect' do
get :index
response.should_not be_redirect
end
end
context 'is not in db' do
context 'session[:cas_user] does not exist' do
it 'should return nil' do
get :index
expect(assigns(:current_user)).to eq(nil)
end
end
it "should redirect_to new_user_path" do
controller.stub(:current_user, redirect: true).and_return(nil)
get :index
response.should be_redirect
end
end
end
The error I'm getting right now is
No route matches {:action=>"index", :controller=>"test"}
I would add the test#index route to config/routes.rb, but it doesn't recognize the Test Controller, so I want to do something like
MyApp::Application.routes.append do
controller :test do
get 'test/index' => :index
end
end
but I'm not sure where to add this or if this even works in rspec. Any help would be great!
If you are trying to test your ApplicationController, see this RSpec documentation about it. You will need to define methods like index inside the test, but it works well.

Why i can not get current_user while writing test case with Rspec and Capybara

I have to write integration test case for my one feature listing page and that feature index method has code like below
def index
#food_categories = current_user.food_categories
end
Now when i try to write a test case for this it throws an error
'undefined method features for nil class' because it can not get the current user
Now what i have do is below
I have write the login process in the before each statement and then write the test case for the features listing page
Can you please let me know that how i can get the current_user ?
FYI, I have used devise gem and working on integration test case with Rspec
Here is my spec file
And here is my food_categories_spec.rb
Update: you confuse functional and integration tests. Integration test doesn't use get, because there's no controller action to test, instead you must use visit (some url). Then you have to examine content of a page, not response code (latter is for functional tests). It may look like:
visit '/food_categories'
page.should have_content 'Eggs'
page.should have_content 'Fats and oils'
In case you'll need functional test, here's an example:
# spec/controllers/your_controller_spec.rb
describe YourController do
before do
#user = FactoryGirl.create(:user)
sign_in #user
end
describe "GET index" do
before do
get :index
end
it "is successful" do
response.should be_success
end
it "assings user features" do
assigns(:features).should == #user.features
end
end
end
# spec/spec_helper.rb
RSpec.configure do |config|
#...
config.include Devise::TestHelpers, :type => :controller
end

How do you "nest" or "group" Test::Unit tests?

RSpec has:
describe "the user" do
before(:each) do
#user = Factory :user
end
it "should have access" do
#user.should ...
end
end
How would you group tests like that with Test::Unit? For example, in my controller test, I want to test the controller when a user is signed in and when nobody is signed in.
You can achieve something similar through classes. Probably someone will say this is horrible but it does allow you to separate tests within one file:
class MySuperTest < ActiveSupport::TestCase
test "something general" do
assert true
end
class MyMethodTests < ActiveSupport::TestCase
setup do
#variable = something
end
test "my method" do
assert object.my_method
end
end
end
Test::Unit, to my knowledge, does not support test contexts. However, the gem contest adds support for context blocks.
Shoulda https://github.com/thoughtbot/shoulda although it looks like they've now made the context-related code into a separate gem: https://github.com/thoughtbot/shoulda-context
Using shoulda-context:
In your Gemfile:
gem "shoulda-context"
And in your test files you can do things like (notice the should instead of test:
class UsersControllerTest < ActionDispatch::IntegrationTest
context 'Logged out user' do
should "get current user" do
get api_current_user_url
assert_response :success
assert_equal response.body, "{}"
end
end
end

Resources