Class: TestMinitestStub

Inherits:
Minitest::Test show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb

Defined Under Namespace

Classes: Bar, Foo, Keywords, Thingy, Time

Constant Summary

Constants inherited from Minitest::Test

Minitest::Test::PASSTHROUGH_EXCEPTIONS, Minitest::Test::SETUP_METHODS, Minitest::Test::TEARDOWN_METHODS

Constants included from Minitest::Assertions

Minitest::Assertions::E, Minitest::Assertions::UNDEFINED

Constants inherited from Minitest::Runnable

Minitest::Runnable::SIGNALS

Instance Attribute Summary

Attributes inherited from Minitest::Runnable

#assertions, #failures, #time

Instance Method Summary collapse

Methods inherited from Minitest::Test

#capture_exceptions, #class_name, #clean, i_suck_and_my_tests_are_order_dependent!, make_my_diffs_pretty!, #neuter_exception, #new_exception, parallelize_me!, #run, runnable_methods, #sanitize_exception, test_order, #with_empty_backtrace_filter, #with_info_handler

Methods included from Minitest::Guard

#jruby?, #maglev?, #mri?, #osx?, #rubinius?, #windows?

Methods included from Minitest::Test::LifecycleHooks

#after_setup, #after_teardown, #before_setup, #before_teardown

Methods included from Minitest::Reportable

#class_name, #error?, #location, #passed?, #result_code, #skipped?

Methods included from Minitest::Assertions

#_synchronize, #assert, #assert_empty, #assert_equal, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_mock, #assert_nil, #assert_operator, #assert_output, #assert_path_exists, #assert_predicate, #assert_raises, #assert_respond_to, #assert_same, #assert_send, #assert_silent, #assert_throws, #capture_io, #capture_subprocess_io, #diff, diff, diff=, #exception_details, #fail_after, #flunk, #message, #mu_pp, #mu_pp_for_diff, #pass, #refute, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_path_exists, #refute_predicate, #refute_respond_to, #refute_same, #skip, #skip_until, #skipped?, #things_to_diff

Methods inherited from Minitest::Runnable

#failure, inherited, #initialize, #marshal_dump, #marshal_load, methods_matching, #name, #name=, on_signal, #passed?, reset, #result_code, run, #run, run_one_method, runnable_methods, runnables, #skipped?, #time_it, #whatever, with_info_handler

Constructor Details

This class inherits a constructor from Minitest::Runnable

Instance Method Details

#assert_deprecated(re = /deprecated/) ⇒ Object



1126
1127
1128
1129
1130
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1126

def assert_deprecated re = /deprecated/
  assert_output "", re do
    yield
  end
end

#assert_stub(val_or_callable) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 612

def assert_stub val_or_callable
  @assertion_count += 1

  t = Time.now.to_i

  Time.stub :now, val_or_callable do
    @tc.assert_equal 42, Time.now
  end

  @tc.assert_operator Time.now.to_i, :>=, t
end

#setupObject

Do not parallelize since we’re calling stub on class methods



593
594
595
596
597
598
599
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 593

def setup
  super
  Minitest::Test.reset

  @tc = Minitest::Test.new "fake tc"
  @assertion_count = 1
end

#skip_stub6Object



1132
1133
1134
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1132

def skip_stub6
  skip "not yet" unless STUB6
end

#teardownObject



601
602
603
604
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 601

def teardown
  super
  assert_equal @assertion_count, @tc.assertions if self.passed?
end

#test_dynamic_methodObject



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 698

def test_dynamic_method
  @assertion_count = 2

  dynamic = Class.new do
    def self.respond_to? meth
      meth == :found
    end

    def self.method_missing meth, *args, &block
      if meth == :found
        false
      else
        super
      end
    end
  end

  val = dynamic.stub(:found, true) do |s|
    s.found
  end

  @tc.assert_equal true, val
  @tc.assert_equal false, dynamic.found
end

#test_mock_with_yieldObject



735
736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 735

def test_mock_with_yield
  mock = Minitest::Mock.new
  mock.expect(:write, true) do
    true
  end
  rs = nil

  File.stub :open, true, mock do
    File.open "foo.txt", "r" do |f|
      rs = f.write
    end
  end
  @tc.assert_equal true, rs
end

#test_mock_with_yield_kwargsObject



750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 750

def test_mock_with_yield_kwargs
  mock = Minitest::Mock.new
  rs = nil

  File.stub :open, true, mock, kw:42 do
    File.open "foo.txt", "r" do |f, kw:|
      rs = kw
    end
  end

  @tc.assert_equal 42, rs
end

#test_stub__hash_as_last_real_argObject



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 848

def test_stub__hash_as_last_real_arg
  with_kwargs_env do
    token = Object.new
    def token.create_with_retry u, p; raise "shouldn't see this"; end

    controller = Object.new
    controller.define_singleton_method :create do |u, p|
      token.create_with_retry u, p
    end

    params = Object.new
    def params.to_hash; raise "nah"; end

    token.stub(:create_with_retry, ->(u, p) { 42 }) do
      act = controller.create :u, params
      @tc.assert_equal 42, act
    end
  end
end

#test_stub_blockObject



662
663
664
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 662

def test_stub_block
  assert_stub lambda { 42 }
end

#test_stub_block_argsObject



666
667
668
669
670
671
672
673
674
675
676
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 666

def test_stub_block_args
  @assertion_count += 1

  t = Time.now.to_i

  Time.stub :now,  lambda { |n| n * 2 } do
    @tc.assert_equal 42, Time.now(21)
  end

  @tc.assert_operator Time.now.to_i, :>=, t
end

#test_stub_callableObject



678
679
680
681
682
683
684
685
686
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 678

def test_stub_callable
  obj = Object.new

  def obj.call
    42
  end

  assert_stub obj
end

#test_stub_callable_block_5Object

from tenderlove



868
869
870
871
872
873
874
875
876
877
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 868

def test_stub_callable_block_5 # from tenderlove
  @assertion_count += 1
  Foo.stub5 :blocking, Bar.new do
    @tc.assert_output "hi\n", "" do
      Foo.blocking do
        @tc.flunk "shouldn't ever hit this"
      end
    end
  end
end

#test_stub_callable_block_6Object

from tenderlove



879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 879

def test_stub_callable_block_6 # from tenderlove
  skip_stub6

  @assertion_count += 1
  Foo.stub6 :blocking, Bar.new do
    @tc.assert_output "hi\n", "" do
      Foo.blocking do
        @tc.flunk "shouldn't ever hit this"
      end
    end
  end
end

#test_stub_callable_keyword_argsObject



842
843
844
845
846
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 842

def test_stub_callable_keyword_args
  Keywords.stub :args, ->(*args, **kws) { [args, kws] } do
    @tc.assert_equal [["woot"], { kw1: 42 }], Keywords.args("woot", kw1: 42)
  end
end

#test_stub_lambdaObject



892
893
894
895
896
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 892

def test_stub_lambda
  Thread.stub :new, lambda { 21+21 } do
    @tc.assert_equal 42, Thread.new
  end
end

#test_stub_lambda_argsObject



898
899
900
901
902
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 898

def test_stub_lambda_args
  Thread.stub :new, lambda { 21+21 }, :wtf do
    @tc.assert_equal 42, Thread.new
  end
end

#test_stub_lambda_block_5Object



904
905
906
907
908
909
910
911
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 904

def test_stub_lambda_block_5
  Thread.stub5 :new, lambda { 21+21 } do
    result = Thread.new do
      @tc.flunk "shouldn't ever hit this"
    end
    @tc.assert_equal 42, result
  end
end

#test_stub_lambda_block_6Object



913
914
915
916
917
918
919
920
921
922
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 913

def test_stub_lambda_block_6
  skip_stub6

  Thread.stub6 :new, lambda { 21+21 } do
    result = Thread.new do
      @tc.flunk "shouldn't ever hit this"
    end
    @tc.assert_equal 42, result
  end
end

#test_stub_lambda_block_args_5Object



924
925
926
927
928
929
930
931
932
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 924

def test_stub_lambda_block_args_5
  @assertion_count += 1
  Thingy.stub5 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
    result = Thingy.identity :nope do |x|
      @tc.flunk "shouldn't reach this"
    end
    @tc.assert_equal 42, result
  end
end

#test_stub_lambda_block_args_6Object



934
935
936
937
938
939
940
941
942
943
944
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 934

def test_stub_lambda_block_args_6
  skip_stub6

  @assertion_count += 1
  Thingy.stub6 :identity, lambda { |y| @tc.assert_equal :nope, y; 21+21 }, :WTF? do
    result = Thingy.identity :nope do |x|
      @tc.flunk "shouldn't reach this"
    end
    @tc.assert_equal 42, result
  end
end

#test_stub_lambda_block_args_6_2Object



946
947
948
949
950
951
952
953
954
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 946

def test_stub_lambda_block_args_6_2
  skip_stub6

  @tc.assert_raises ArgumentError do
    Thingy.stub6_2 :identity, lambda { |y| :__not_run__ }, :WTF? do
      # doesn't matter
    end
  end
end

#test_stub_lambda_block_call_5Object



956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 956

def test_stub_lambda_block_call_5
  @assertion_count += 1
  rs = nil
  io = StringIO.new "", "w"
  File.stub5 :open, lambda { |p, m, &blk| blk and blk.call io } do
    File.open "foo.txt", "r" do |f|
      rs = f && f.write("woot")
    end
  end
  @tc.assert_equal 4, rs
  @tc.assert_equal "woot", io.string
end

#test_stub_lambda_block_call_6Object



969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 969

def test_stub_lambda_block_call_6
  skip_stub6

  @assertion_count += 1
  rs = nil
  io = StringIO.new "", "w"
  File.stub6 :open, lambda { |p, m, &blk| blk.call io } do
    File.open "foo.txt", "r" do |f|
      rs = f.write("woot")
    end
  end
  @tc.assert_equal 4, rs
  @tc.assert_equal "woot", io.string
end

#test_stub_lambda_block_call_args_5Object



984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 984

def test_stub_lambda_block_call_args_5
  @assertion_count += 1
  rs = nil
  io = StringIO.new "", "w"
  File.stub5(:open, lambda { |p, m, &blk| blk and blk.call io }, :WTF?) do
    File.open "foo.txt", "r" do |f|
      rs = f.write("woot")
    end
  end
  @tc.assert_equal 4, rs
  @tc.assert_equal "woot", io.string
end

#test_stub_lambda_block_call_args_6Object



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 997

def test_stub_lambda_block_call_args_6
  skip_stub6

  @assertion_count += 1
  rs = nil
  io = StringIO.new "", "w"
  File.stub6(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
    File.open "foo.txt", "r" do |f|
      rs = f.write("woot")
    end
  end
  @tc.assert_equal 4, rs
  @tc.assert_equal "woot", io.string
end

#test_stub_lambda_block_call_args_6_2Object



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1012

def test_stub_lambda_block_call_args_6_2
  skip_stub6

  @assertion_count += 2
  rs = nil
  io = StringIO.new "", "w"
  @tc.assert_raises ArgumentError do
    File.stub6_2(:open, lambda { |p, m, &blk| blk.call io }, :WTF?) do
      File.open "foo.txt", "r" do |f|
        rs = f.write("woot")
      end
    end
  end
  @tc.assert_nil rs
  @tc.assert_equal "", io.string
end

#test_stub_NameErrorObject



723
724
725
726
727
728
729
730
731
732
733
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 723

def test_stub_NameError
  e = @tc.assert_raises NameError do
    Time.stub :nope_nope_nope, 42 do
      # do nothing
    end
  end

  exp = jruby? ? /Undefined method nope_nope_nope for '#{self.class}::Time'/ :
    /undefined method `nope_nope_nope' for( class)? `#{self.class}::Time'/
  assert_match exp, e.message
end

#test_stub_private_module_methodObject



624
625
626
627
628
629
630
631
632
633
634
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 624

def test_stub_private_module_method
  @assertion_count += 1

  t0 = Time.now

  self.stub :sleep, nil do
    @tc.assert_nil sleep(10)
  end

  @tc.assert_operator Time.now - t0, :<=, 1
end

#test_stub_private_module_method_indirectObject



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 636

def test_stub_private_module_method_indirect
  @assertion_count += 1

  fail_clapper = Class.new do
    def fail_clap
      raise
      :clap
    end
  end.new

  fail_clapper.stub :raise, nil do |safe_clapper|
    @tc.assert_equal :clap, safe_clapper.fail_clap # either form works
    @tc.assert_equal :clap, fail_clapper.fail_clap # yay closures
  end
end

#test_stub_public_module_methodObject



652
653
654
655
656
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 652

def test_stub_public_module_method
  Math.stub :log10, :stubbed do
    @tc.assert_equal :stubbed, Math.log10(1000)
  end
end

#test_stub_valueObject



658
659
660
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 658

def test_stub_value
  assert_stub 42
end

#test_stub_value__oldObject

TODO: remove/rename



763
764
765
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 763

def test_stub_value
  assert_stub 42
end

#test_stub_value_argsObject



1036
1037
1038
1039
1040
1041
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1036

def test_stub_value_args
  Thread.stub :new, 42, :WTF? do
    result = Thread.new
    @tc.assert_equal 42, result
  end
end

#test_stub_value_block_5Object



1043
1044
1045
1046
1047
1048
1049
1050
1051
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1043

def test_stub_value_block_5
  @assertion_count += 1
  Thread.stub5 :new, 42 do
    result = Thread.new do
      @tc.assert true
    end
    @tc.assert_equal 42, result
  end
end

#test_stub_value_block_6Object



1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1053

def test_stub_value_block_6
  skip_stub6

  Thread.stub6 :new, 42 do
    result = Thread.new do
      @tc.flunk "shouldn't hit this"
    end
    @tc.assert_equal 42, result
  end
end

#test_stub_value_block_args_5Object



1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1064

def test_stub_value_block_args_5
  @assertion_count += 2
  rs = nil
  io = StringIO.new "", "w"
  File.stub5 :open, :value, io do
    result = File.open "foo.txt", "r" do |f|
      rs = f.write("woot")
    end
    @tc.assert_equal :value, result
  end
  @tc.assert_equal 4, rs
  @tc.assert_equal "woot", io.string
end

#test_stub_value_block_args_5__break_if_not_passedObject



1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1078

def test_stub_value_block_args_5__break_if_not_passed
  e = @tc.assert_raises NoMethodError do
    File.stub5 :open, :return_value do # intentionally bad setup w/ no args
      File.open "foo.txt", "r" do |f|
        f.write "woot"
      end
    end
  end
  exp = "undefined method `write' for nil:NilClass"
  assert_match exp, e.message
end

#test_stub_value_block_args_6Object



1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1090

def test_stub_value_block_args_6
  skip_stub6

  @assertion_count += 2
  rs = nil
  io = StringIO.new "", "w"
  assert_deprecated do
    File.stub6 :open, :value, io do
      result = File.open "foo.txt", "r" do |f|
        rs = f.write("woot")
      end
      @tc.assert_equal :value, result
    end
  end
  @tc.assert_equal 4, rs
  @tc.assert_equal "woot", io.string
end

#test_stub_value_block_args_6_2Object



1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 1108

def test_stub_value_block_args_6_2
  skip_stub6

  @assertion_count += 2
  rs = nil
  io = StringIO.new "", "w"
  @tc.assert_raises ArgumentError do
    File.stub6_2 :open, :value, io do
      result = File.open "foo.txt", "r" do |f|
        @tc.flunk "shouldn't hit this"
      end
      @tc.assert_equal :value, result
    end
  end
  @tc.assert_nil rs
  @tc.assert_equal "", io.string
end

#test_stub_yield_selfObject



688
689
690
691
692
693
694
695
696
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/minitest-5.17.0/test/minitest/test_minitest_mock.rb', line 688

def test_stub_yield_self
  obj = "foo"

  val = obj.stub :to_s, "bar" do |s|
    s.to_s
  end

  @tc.assert_equal "bar", val
end