I'm building a rails 4 app. I created a support file to simulate a login. Here are the files
module SpecTestHelper
def login(user)
request.session[:user_id] = user.id
end
def current_user
User.find(request.session[:user_id])
end
end
config.include SpecTestHelper, :type => :controller
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With