rails minitest bdd how to specify which test superclass to use - ruby-on-rails

Using a Grape API and add the tests in the Rakefile. Its a Rack application mounted on the main rails app.
namespace :test do
Rake::TestTask.new(:api) do |t|
t.pattern = 'test/api/**/*_test.rb'
end
end
Rake::Task[:test].enhance [ 'test:api' ]
However if I try to test it there aren't any of the controller methods.
describe API do
# get, post, patch etc all raise
end
Error below.
NoMethodError: undefined method `get' for #<#<Class:0x007fd63cdfc0e0>:0x007fd63cdc7a70>
How can I test rack apps with minitest-rails bdd?

Figured it out.
class MiniTest::Spec
include FactoryGirl::Syntax::Methods
include Rack::Test::Methods
def app
API
end
end

Related

Tell rspec (core) that test is testing controller

Trying to run this test but keep getting the following error:
Failure/Error: get :index
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::TestModuleTestController::Controller:0x007fa4bc120d00>
Note: I'm not using rspec-rails.
require "spec_helper"
module TestModule
describe TestController, :type => :controller do
describe "controller" do
it "sets X-Frame-Options to ALLOWALL" do
get :index
expect(response.headers['X-Frame-Options']).to eq('ALLOWALL')
end
end
end
end
Note: I'm not using rspec-rails.
That's your problem right there. All the rails type specs (controller, request, features, views) are part of rspec-rails not rspec-core.
Without rspec-rails the type metadata does absolutely nothing - its just a plain example group describing a class.
The solution is to add rspec-rails to your gemfile.
group :development, :test do
gem 'rspec-rails', '~> 3.6'
end
And run rails g rspec install.
https://github.com/rspec/rspec-rails

NoMethodError attempting to access fixture

I am working on migrating the test suite for my project to rspec. I am getting the following error trying to access my fixtures:
undefined method `[]' for nil:NilClass
Here is an example spec file:
require 'rails_helper'
feature "edit contacts" do
fixtures :contacts
before(:all) do
#thing = contacts(:two)
end
scenario "do a thing" do
visit new_contact_path
end
end
I get the error in the before block, on the call to contacts(:two)
Here is config info from rails_helper.rb
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.fixture_path = "#{::Rails.root}/spec/fixtures"
end
Here is version info from Gemfile.lock:
rails (4.0.13)
rspec-core (3.2.2)
rspec-expectations (3.2.0)
rspec-mocks (3.2.1)
rspec-rails (3.2.1)
rspec-support (3.2.2)
I'm happy to provide any additional supporting information if needed. Thanks for taking a look, any help appreciated!
ok - finally resolved this with the help of a coworker. The issue was that I should have been using before(:each) instead of before(:all).
Apparently, using :each triggers a 'before_setup' callback in ActiveRecord::FixtureSet which sets up all the fixtures, and :all doesn't.
According to the docs:
before(:each) blocks are run before each example before(:all) blocks are run once before all of the examples in a group

Using Devise's current_user in Test Unit

In some of my controllers I need to use the group(s) that belong to my logged in user(current_user.groups). When I try to test; I don't seem to have this current_user though:
ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
So I figured I should create that current_user with Devise.
I've read the documentation of Devise stating I should add the following to my test_helper.rb:
class ActionController::TestCase
include Devise::TestHelpers
def setup
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in FactoryGirl.create(:user)
end
end
This doesn't seem to do the trick though; Whenever I run rake test I get the following errors:
1) Error:
ActivitiesControllerTest#test_should_create_activity:
NameError: uninitialized constant ActionController::TestCase::FactoryGirl
test/test_helper.rb:22:in `setup'
You have to include the factory_girl_rails gem in your Gemfile. I usually include it in both the development and test group, but just the test environment is fine for your example.
group :development, :test do
gem 'factory_girl_rails'
end
And then run bundle install.
factory_girl_rails is used when you are creating the user fixture in your test:
sign_in FactoryGirl.create(:user)
Then you need to create a factory (which is almost like a fixture):
rails generate factory_girl:model user
This will create the file: test/factories/users.rb
Read more about factory_girl_rails and how to define factories here: https://github.com/thoughtbot/factory_girl_rails

How to test simple rake application

i am using grape for creating rest api i created the api and its working fine now i have to test this api.when we create rails api there is automatically spec_helper.rb file is generated now as usual first line for testing is
require spec_helper
please tell what should be the code for spec_helper.rb file
and other things i should focus when testing a simple rake application.i am giving a small code snippet for example i have to test
require 'grape'
require 'sequel'
require 'json'
module Twitter
class API < Grape::API
version 'v1', :using => :header, :vendor => 'twitter'
format :json
helpers do
def current_user
#current_user ||= User.authorize!(env)
end
def authenticate!
error!('401 Unauthorized', 401) unless current_user
end
end
resource :users do
desc "Return a status."
params do
requires :id, :type => Integer, :desc => "Status id."
optional :include , :type => String , :desc =>"parameter to include in "
end
get ':id' do
"Hello World"
end
in this grape application when i call localhost:9292/users/1234
then response should be "Hello World" how to test this app what should be content of spec_helper.rb file for testing.i am using only grape not using rails
It all depends on what you want to test.
Presumably the route you want to test (localhost:9292/users/1234) is a UsersController. That being the case, you will want to do something like this (using rspec):
describe UsersController do
context "GET#show" do
it "should return 'Hello World'" do
get :show, id: 1234
response.body.should include 'Hello World'
end
end
end
Now as for the rake task test, I would create an integration test that executes the rake task from the command-line and compares expected results to the output of the rake task sort of like this:
describe "My Rake Task" do
it "should return hello world" do
results = `bundle exec rake my:rake:task`
results.should include 'Hello World'
end
end
Hope these rough examples work for you! Good luck!
UPDATE:
You should always write unit test on classes as much as possible so your rake task tests are very simple or not even needed.
I think you mean Rack app. There's a pretty decent section on testing in the README for Grape. You should start there.
https://github.com/intridea/grape#writing-tests

problem with rspec test, undefined method 'post'

I am writing a spec to test the behavior of the mashup_controller when someone sends a query through a URL. I need to simulate the parameters contained in the URL, and i read that the post() method will do that, however when i get an error:
1) MashupController simulates query
Failure/Error: post :create
NoMethodError:
undefined method `post' for
#<RSpec::Core::ExampleGroup::Nested_1:0x980bc50>
# ./mashup_controller_rspec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.20199 seconds 1 example, 1 failure
Failed examples:
rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query
Here is my code:
require 'spec_helper'
require 'mashup_controller.rb'
describe MashupController do
it "simulates query" do
post :create
end
end
Sorry if I'm not making any sense. I am very new to rails and rspec. Any help would be appreciated. Thanks.
If the spec file is not under spec/controllers, methods like get and post will not be automatically made available by rspec-rails.
You either need to tag your spec:
describe MyController, type: :controller do
# ...
end
or include the module:
describe MyController do
include RSpec::Rails::ControllerExampleGroup
# ...
end
See the relevant code in rspec-rails.
Make sure you have gem spec-rails in your Gemfile
Your mashup_controller_rspec.rb should be under spec/controllers
I used gem rspec-rails instead of gem spec-rails.
In Rails 4, you can declare the type of the RSpec tests as :request and the spec file can be in any directory.
example: in spec/routes/users.rb
RSpec.describe 'UserRoutes', type: :request do
...
end
My solution is
describe MyController, type: :controller
...
end

Resources