minitest

How do I stub a method to raise an error using Ruby MiniTest?

家住魔仙堡 提交于 2021-02-07 11:13:57
问题 I'm trying to test a Rails controller branch that is triggered when the model method raises an error. def my_controller_method @my_object = MyObject.find(params[:id]) begin result = @my_object.my_model_method(params) rescue Exceptions::CustomError => e flash.now[:error] = e.message redirect_to my_object_path(@my_object) and return end # ... rest irrelevant end How can I get a Minitest stub to raise this Error? it 'should show redirect on custom error' do my_object = FactoryGirl.create(:my

ActionView::Template::Error: ActionView::Template::Error: undefined method `[]' for nil:NilClass

♀尐吖头ヾ 提交于 2021-02-07 08:01:48
问题 I have a basic static webpage. class StaticPagesController < ApplicationController def home end end And there is a home.html.erb with just heading. This works perfectly fine in developement but the test test "should get home" do get :home assert_response :success assert_select "title","home | #{@base}" end is failing with error ActionView::Template::Error: ActionView::Template::Error: undefined method []' for nil:NilClass Its showing error in application.html.erb <%= stylesheet_link_tag

ActionView::Template::Error: ActionView::Template::Error: undefined method `[]' for nil:NilClass

旧巷老猫 提交于 2021-02-07 08:01:42
问题 I have a basic static webpage. class StaticPagesController < ApplicationController def home end end And there is a home.html.erb with just heading. This works perfectly fine in developement but the test test "should get home" do get :home assert_response :success assert_select "title","home | #{@base}" end is failing with error ActionView::Template::Error: ActionView::Template::Error: undefined method []' for nil:NilClass Its showing error in application.html.erb <%= stylesheet_link_tag

How is a test throwing InvalidAuthenticityToken when forgery protection is disabled in the test environment?

房东的猫 提交于 2020-07-09 12:49:20
问题 In my ApplicationController , I have set: protect_from_forgery with: :exception In my config/environments/test.rb , I have set: config.action_controller.allow_forgery_protection = false My understanding is that this should prevent authenticity token checking. But when running my tests, my non- GET requests result in: Minitest::UnexpectedError: ActionController::InvalidAuthenticityToken If I comment out protect_from_forgery with: :exception , I no longer get the InvalidAuthenticityToken

Using helpers in minitest

为君一笑 提交于 2020-01-24 01:11:08
问题 I'm trying to test my app class in using minitest but I am getting a error stating undefined method image_path . How can I get around this? app_test.rb require 'test_helper' class AppTest < ActiveSupport::TestCase setup do @app = apps(:app_one) end test 'should have icon_url' do assert(@app.icon_url == image_path('icn-medium-norm.png')) end end app/models/app.rb class App < ActiveRecord::Base has_many :versions, dependent: :destroy has_many :user_subscriptions, dependent: :destroy has_many

Using Minitest in Rails

和自甴很熟 提交于 2020-01-20 17:03:13
问题 Recently, I've read quite a few articles about Minitest. I really like the idea of a super lightweight test framework. I decided to replace rspec with it in a recent project and have had no luck getting it all to work. My problems are a) getting named routes in my acceptance/integration tests (rspec and test::unit seem to automatically include them but no go with minitest), b) and the overall lack of adoption in rails makes me uneasy (everyone seems to be using rspec though it's used more

Using Minitest in Rails

倖福魔咒の 提交于 2020-01-20 17:00:27
问题 Recently, I've read quite a few articles about Minitest. I really like the idea of a super lightweight test framework. I decided to replace rspec with it in a recent project and have had no luck getting it all to work. My problems are a) getting named routes in my acceptance/integration tests (rspec and test::unit seem to automatically include them but no go with minitest), b) and the overall lack of adoption in rails makes me uneasy (everyone seems to be using rspec though it's used more

Using Minitest in Rails

守給你的承諾、 提交于 2020-01-20 16:58:11
问题 Recently, I've read quite a few articles about Minitest. I really like the idea of a super lightweight test framework. I decided to replace rspec with it in a recent project and have had no luck getting it all to work. My problems are a) getting named routes in my acceptance/integration tests (rspec and test::unit seem to automatically include them but no go with minitest), b) and the overall lack of adoption in rails makes me uneasy (everyone seems to be using rspec though it's used more

How do I log in a user with Devise for Rails controller/integration unit tests?

China☆狼群 提交于 2020-01-17 06:57:06
问题 All the other answers are wrong or out of date. I've tried several things. This is what I have: require 'test_helper' class DealsControllerTest < ActionDispatch::IntegrationTest include Devise::Test::IntegrationHelpers setup do @deal = deals(:one) @user = users(:one) # https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec) #@request.env["devise.mapping"] = Devise.mappings[:one] #sign_in @user #log_in_as @user ApplicationController.allow_forgery

Rails 4 Minitest Testing Virtual Attributes

邮差的信 提交于 2020-01-17 05:38:22
问题 So I have a class TimeBank, and its being updated, and is getting two new virtual attributes which will manipulate two other existing attributes (DB columns). class TimeBank < ActiveRecord::Base # validations, consts, few other methods omitted for brevity def date_worked @date_worked ||= self.start.to_date rescue Date.current end def date_worked=(date_worked_val) self.attributes['date_worked'] = date_worked_val end def hours_worked @hours_worked ||= hours rescue NoMethodError nil end def