Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a support file for rspec

Tags:

I'm building a rails 4 app. I created a support file to simulate a login. Here are the files

spec/support/spec_test_helper.rb

module SpecTestHelper
  def login(user)
    request.session[:user_id] = user.id
  end

  def current_user
    User.find(request.session[:user_id])
  end
end

spec_helper.rb

config.include SpecTestHelper, :type => :controller

controller spec

describe BooksController, "user role" do

  user = Fabricate(:user) do
    role { Role.find_by_account_type("user") }
  end

  login(user)
end

The support file gives an undefined method error. This is part of the error message:

spec/controllers/books_controller_spec.rb:27:in `block in <top (required)>': undefined method `login' for #<Class:0x007f9f83193438> (NoMethodError)

I'm testing CanCan. I know the correct way to test CanCan is testing the Ability but that's already done.

like image 292
Ricbermo Avatar asked Dec 16 '13 15:12

Ricbermo


People also ask

How do I run an RSpec on a specific file?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

How do I set up RSpec?

Installing RSpecBoot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.

Is RSpec TDD or BDD?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

Where is spec folder in Rails?

Request specs reside in the spec/requests directory. The directory can also be named integration or api . Feature specs reside in the spec/features directory.


1 Answers

I added this line in spec_helper.rb and it works in 3rd Rails

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

Maybe another (more pretty) solution exists

like image 100
gotva Avatar answered Oct 04 '22 16:10

gotva