1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
|
#!/usr/bin/env ruby
#---
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
# All rights reserved.
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#+++
require 'test_helper'
def mock_top_level_function
:mtlf
end
module Kernel
def mock_kernel_function
:mkf
end
end
# Used for testing
class Cat
def purr
end
def meow
end
end
class TestFlexMockShoulds < Minitest::Test
include FlexMock::Minitest
# Expected error messages on failures
COUNT_ERROR_MESSAGE = /\bcalled\s+incorrect\s+number\s+of\s+times\b/
NO_MATCH_ERROR_MESSAGE = /\bno\s+matching\s+handler\b/
AT_LEAST_ERROR_MESSAGE = COUNT_ERROR_MESSAGE
AT_MOST_ERROR_MESSAGE = COUNT_ERROR_MESSAGE
OUT_OF_ORDER_ERROR_MESSAGE = /\bcalled\s+out\s+of\s+order\b/
NON_CONTAINER_MESSAGE = /\bis\s+not\s+in\s+a\s+container\b/
def test_defaults
FlexMock.use do |m|
m.should_receive(:hi)
assert_nil m.hi
assert_nil m.hi(1)
assert_nil m.hi("hello", 2)
end
end
def test_returns_with_value
FlexMock.use do |m|
m.should_receive(:hi).returns(1)
assert_equal 1, m.hi
assert_equal 1, m.hi(123)
end
end
def test_returns_with_multiple_values
FlexMock.use do |m|
m.should_receive(:hi).and_return(1,2,3)
assert_equal 1, m.hi
assert_equal 2, m.hi
assert_equal 3, m.hi
assert_equal 3, m.hi
assert_equal 3, m.hi
end
end
def test_multiple_returns
FlexMock.use do |m|
m.should_receive(:hi).and_return(1).and_return(2,3)
assert_equal 1, m.hi
assert_equal 2, m.hi
assert_equal 3, m.hi
assert_equal 3, m.hi
assert_equal 3, m.hi
end
end
def test_returns_with_block
FlexMock.use do |m|
result = nil
m.should_receive(:hi).with(Object).returns { |obj| result = obj }
m.hi(3)
assert_equal 3, result
end
end
def test_with_block_example_from_readme
FlexMock.use do |m|
m.should_receive(:foo).with(Integer).with_block.and_return(:got_block)
m.should_receive(:foo).with(Integer).and_return(:no_block)
assert_equal :no_block, m.foo(1)
assert_equal :got_block, m.foo(1) { }
end
end
def test_with_no_block_example_from_readme
FlexMock.use do |m|
m.should_receive(:foo).with(Integer).with_no_block.and_return(:no_block)
m.should_receive(:foo).with(Integer).and_return(:got_block)
assert_equal :no_block, m.foo(1)
assert_equal :got_block, m.foo(1) { }
end
end
def test_with_optional_block
FlexMock.use do |m|
m.should_receive(:foo).with(Integer).with_optional_block.twice
m.foo(1)
m.foo(1) {}
end
end
def test_return_with_and_without_block_interleaved
FlexMock.use do |m|
m.should_receive(:hi).and_return(:a).and_return { :b }.and_return(:c)
assert_equal :a, m.hi
assert_equal :b, m.hi
assert_equal :c, m.hi
assert_equal :c, m.hi
end
end
def test_and_returns_alias
FlexMock.use do |m|
m.should_receive(:hi).and_return(4)
assert_equal 4, m.hi
end
end
def test_and_return_undefined
FlexMock.use do |m|
m.should_receive(:foo).and_return_undefined
m.should_receive(:phoo).returns_undefined
assert_equal FlexMock.undefined, m.foo
assert_equal FlexMock.undefined, m.foo.bar.baz.bing.ka_ching
assert_equal FlexMock.undefined, m.phoo.bar.baz.bing.ka_ching
end
end
def test_and_yield_will_continue_to_yield_the_same_value
FlexMock.use do |m|
m.should_receive(:hi).and_yield(:yield_value)
assert_equal :yield_value, m.hi { |v| v }
assert_equal :yield_value, m.hi { |v| v }
end
end
def test_and_yield_with_multiple_values_yields_the_values
FlexMock.use do |m|
m.should_receive(:hi).and_yield(:one, :two).once
assert_equal [:one, :two], m.hi { |a, b| [a, b] }
end
end
def test_multiple_yields_are_done_sequentially
FlexMock.use do |m|
m.should_receive(:msg).and_yield(:one).and_yield(:two)
assert_equal :one, m.msg { |a| a }
assert_equal :two, m.msg { |a| a }
assert_equal :two, m.msg { |a| a }
end
end
def test_multiple_yields_and_multiple_returns_are_synced
FlexMock.use do |m|
m.should_receive(:msg).and_yield(:one).and_return(1).and_yield(:two).and_return(2)
yielded_values = []
returned_values = []
returned_values << m.msg { |a| yielded_values << a }
returned_values << m.msg { |a| yielded_values << a }
returned_values << m.msg { |a| yielded_values << a }
assert_equal [:one, :two, :two], yielded_values
assert_equal [1, 2, 2], returned_values
end
end
def test_iteration_yields_values_in_sequence
FlexMock.use do |m|
m.should_receive(:msg).and_iterates(1, 2, 3)
yielded_values = []
m.msg { |a| yielded_values << a }
assert_equal [1, 2, 3], yielded_values
end
end
def test_iteration_and_yields_are_queued
FlexMock.use do |m|
m.should_receive(:msg).
and_yield(:one).
and_iterates(1, 2, 3).
and_yield(:two)
yielded_values = []
yielded_values << m.enum_for(:msg).to_a
yielded_values << m.enum_for(:msg).to_a
yielded_values << m.enum_for(:msg).to_a
assert_equal [[:one], [1, 2, 3], [:two]], yielded_values
end
end
def test_failure_if_no_block_given
FlexMock.use do |m|
m.should_receive(:hi).and_yield(:one, :two).once
assert_raises(FlexMock::MockError) do m.hi end
end
end
def test_failure_different_return_value_than_yield_return
FlexMock.use do |m|
m.should_receive(:hi).and_yield(:yld).once.and_return(:ret)
yielded_value = nil
assert_equal :ret, m.hi { |v| yielded_value = v }
assert_equal :yld, yielded_value
end
end
def test_multiple_yields
FlexMock.use do |m|
m.should_receive(:hi).and_yield(:one, :two).and_yield(1, 2)
assert_equal [:one, :two], m.hi { |a, b| [a, b] }
assert_equal [1, 2], m.hi { |a, b| [a, b] }
end
end
def test_multiple_yields_will_yield_the_last_value_set
FlexMock.use do |m|
m.should_receive(:hi).and_yield(:a).and_yield(:b)
assert_equal [:a], m.hi { |a, b| [a] }
assert_equal [:b], m.hi { |a, b| [a] }
assert_equal [:b], m.hi { |a, b| [a] }
assert_equal [:b], m.hi { |a, b| [a] }
assert_equal [:b], m.hi { |a, b| [a] }
end
end
def test_yielding_then_not_yielding_and_then_yielding_again
FlexMock.use do |m|
m.should_receive(:hi).and_yield(:a).once
m.should_receive(:hi).and_return(:b).once
m.should_receive(:hi).and_yield(:c).once
assert_equal :a, m.hi { |v| v }
assert_equal :b, m.hi
assert_equal :c, m.hi { |v| v }
end
end
def test_yields_syntax
FlexMock.use do |m|
m.should_receive(:hi).yields(:one)
assert_equal :one, m.hi { |a| a }
end
end
class MyError < RuntimeError
end
def test_and_raises_with_exception_class_throws_exception
FlexMock.use do |m|
m.should_receive(:failure).and_raise(MyError)
assert_raises MyError do
m.failure
end
end
end
def test_and_raises_with_arguments_throws_exception_made_with_args
FlexMock.use do |m|
m.should_receive(:failure).and_raise(MyError, "my message")
ex = assert_raises MyError do
m.failure
end
assert_equal "my message", ex.message
end
end
def test_and_raises_with_a_specific_exception_throws_the_exception
FlexMock.use do |m|
err = MyError.new
m.should_receive(:failure).and_raise(err)
ex = assert_raises MyError do
m.failure
end
assert_equal err, ex
end
end
def test_raises_is_an_alias_for_and_raise
FlexMock.use do |m|
m.should_receive(:failure).raises(RuntimeError)
assert_raises RuntimeError do
m.failure
end
end
end
def test_multiple_and_raise_clauses_will_be_done_sequentially
FlexMock.use do |m|
m.should_receive(:failure).
and_raise(RuntimeError, "ONE").
and_raise(RuntimeError, "TWO")
ex = assert_raises RuntimeError do m.failure end
assert_equal "ONE", ex.message
ex = assert_raises RuntimeError do m.failure end
assert_equal "TWO", ex.message
end
end
def test_and_throw_will_throw_a_symbol
FlexMock.use do |m|
m.should_receive(:msg).and_throw(:sym)
value = catch(:sym) do
m.msg
fail "Should not reach this line"
end
assert_nil value
end
end
def test_and_throw_with_expression_will_throw
FlexMock.use do |m|
m.should_receive(:msg).and_throw(:sym, :return_value)
value = catch(:sym) do
m.msg
fail "Should not reach this line"
end
assert_equal :return_value, value
end
end
def test_throws_is_an_alias_for_and_throw
FlexMock.use do |m|
m.should_receive(:msg).throws(:sym, :return_value)
value = catch(:sym) do
m.msg
fail "Should not reach this line"
end
assert_equal :return_value, value
end
end
def test_multiple_throws_will_be_done_sequentially
FlexMock.use do |m|
m.should_receive(:toss).
and_throw(:sym, "ONE").
and_throw(:sym, "TWO")
value = catch(:sym) do m.toss end
assert_equal "ONE", value
value = catch(:sym) do m.toss end
assert_equal "TWO", value
end
end
def test_pass_thru_just_returns_undefined_on_mocks
FlexMock.use do |m|
m.should_receive(:hi).pass_thru
assert_equal FlexMock.undefined, m.hi
end
end
def test_multiple_expectations
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10)
m.should_receive(:hi).with(2).returns(20)
assert_equal 10, m.hi(1)
assert_equal 20, m.hi(2)
end
end
def test_with_no_args_with_no_args
FlexMock.use do |m|
m.should_receive(:hi).with_no_args
m.hi
end
end
def test_with_no_args_but_with_args
assert_mock_failure(check_failed_error, :message =>NO_MATCH_ERROR_MESSAGE, :deep => true) do
FlexMock.use do |m|
m.should_receive(:hi).with_no_args
m.hi(1)
end
end
end
def test_with_any_args
FlexMock.use do |m|
m.should_receive(:hi).with_any_args
m.hi
m.hi(1)
m.hi(1,2,3)
m.hi("this is a test")
end
end
def test_with_any_single_arg_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(1,FlexMock.any).twice
m.hi(1,2)
m.hi(1, "this is a test")
end
end
def test_with_any_single_arg_nonmatching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).times(3)
m.should_receive(:hi).with(1,FlexMock.any).never
m.hi
m.hi(1)
m.hi(1, "hi", nil)
end
end
def test_with_equal_arg_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(FlexMock.eq(Object)).once
m.hi(Object)
end
end
def test_with_ducktype_arg_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(FlexMock.ducktype(:purr, :meow)).once
m.hi(Cat.new)
end
end
def test_with_ducktype_arg_matching_no_match
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(FlexMock.ducktype(:purr, :meow, :growl))
assert_mock_failure(check_failed_error, :deep => true, :line => __LINE__+1) {
m.hi(Cat.new)
}
end
end
def test_with_kw_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(1, a: 2).once
m.hi(1, a: 2)
end
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with_kw_args(FlexMock.hsh(:a => 1, :b => 2)).once
m.hi(:a => 1, :b => 2, :c => 3)
end
end
def test_with_kw_not_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(1, a: 2)
assert_mock_failure(check_failed_error, :deep => true, :line => __LINE__+1) {
m.hi(1, a: 2, b: 3)
}
end
end
def test_with_hash_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(FlexMock.hsh(:a => 1, :b => 2)).once
m.hi({:a => 1, :b => 2, :c => 3}, **{})
end
end
def test_with_hash_non_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(FlexMock.hsh(:a => 1, :b => 2))
assert_mock_failure(check_failed_error, :deep => true, :line => __LINE__+1) {
m.hi({:a => 1, :b => 4, :c => 3}, **{})
}
end
end
def test_with_equal_arg_nonmatching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(FlexMock.eq(Object)).never
m.should_receive(:hi).never
m.should_receive(:hi).with(1).once
m.hi(1)
end
end
def test_with_arbitrary_arg_matching
FlexMock.use('greeter') do |m|
m.should_receive(:hi).with(FlexMock.on { |arg| arg % 2 == 0 rescue nil }).twice
m.should_receive(:hi).never
m.should_receive(:hi).with(1).once
m.should_receive(:hi).with(2).never
m.should_receive(:hi).with(3).once
m.should_receive(:hi).with(4).never
m.hi(1)
m.hi(2)
m.hi(3)
m.hi(4)
end
end
def test_args_matching_with_regex
FlexMock.use do |m|
m.should_receive(:hi).with(/one/).returns(10)
m.should_receive(:hi).with(/t/).returns(20)
assert_equal 10, m.hi("one")
assert_equal 10, m.hi("done")
assert_equal 20, m.hi("two")
assert_equal 20, m.hi("three")
end
end
def test_arg_matching_with_regex_matching_non_string
FlexMock.use do |m|
m.should_receive(:hi).with(/1/).returns(10)
assert_equal 10, m.hi(319)
end
end
def test_arg_matching_with_class
FlexMock.use do |m|
m.should_receive(:hi).with(0.class).returns(10)
m.should_receive(:hi).with(Object).returns(20)
assert_equal 10, m.hi(319)
assert_equal 10, m.hi(0.class)
assert_equal 20, m.hi("hi")
end
end
def test_arg_matching_with_no_match
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10)
assert_mock_failure(check_failed_error, :message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
m.hi(2)
}
end
end
def test_arg_matching_with_string_doesnt_over_match
FlexMock.use do |m|
m.should_receive(:hi).with(String).returns(20)
assert_mock_failure(check_failed_error, :message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
m.hi(1.0)
}
end
end
def test_block_arg_given_to_no_block
FlexMock.use do |m|
m.should_receive(:hi).with_no_block.returns(20)
assert_mock_failure(check_failed_error, :message =>NO_MATCH_ERROR_MESSAGE, :deep => true, :line => __LINE__+1) {
m.hi { 1 }
}
end
end
def test_block_arg_given_to_matching_proc
FlexMock.use do |m|
arg = nil
m.should_receive(:hi)
.with_block.once
.and_return { |&block| arg = block; block.call }
result = m.hi { 1 }
assert_equal 1, arg.call
assert_equal 1, result
end
end
def test_arg_matching_precedence_when_best_first
FlexMock.use("greeter") do |m|
m.should_receive(:hi).with(1).once
m.should_receive(:hi).with(FlexMock.any).never
m.hi(1)
end
end
def test_arg_matching_precedence_when_best_last_but_still_matches_first
FlexMock.use("greeter") do |m|
m.should_receive(:hi).with(FlexMock.any).once
m.should_receive(:hi).with(1).never
m.hi(1)
end
end
def test_never_and_never_called
FlexMock.use do |m|
m.should_receive(:hi).with(1).never
end
end
def test_never_and_called_once
assert_mock_failure(check_failed_error, :message =>COUNT_ERROR_MESSAGE, :deep => true, :line => __LINE__+3) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).never
m.hi(1)
end
end
end
def test_once_called_once
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).once
m.hi(1)
end
end
def test_once_but_never_called
assert_mock_failure(assertion_failed_error, :message =>COUNT_ERROR_MESSAGE, :line => __LINE__+2) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).once
end
end
end
def test_once_but_called_twice
assert_mock_failure(check_failed_error, :message =>COUNT_ERROR_MESSAGE, :line => __LINE__+4) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).once
m.hi(1)
m.hi(1)
end
end
end
def test_twice_and_called_twice
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).twice
m.hi(1)
m.hi(1)
end
end
def test_zero_or_more_called_zero
FlexMock.use do |m|
m.should_receive(:hi).zero_or_more_times
end
end
def test_zero_or_more_called_once
FlexMock.use do |m|
m.should_receive(:hi).zero_or_more_times
m.hi
end
end
def test_zero_or_more_called_100
FlexMock.use do |m|
m.should_receive(:hi).zero_or_more_times
100.times { m.hi }
end
end
def test_times
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).times(10)
10.times { m.hi(1) }
end
end
def test_at_least_called_once
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once
m.hi(1)
end
end
def test_at_least_but_never_called
assert_mock_failure(assertion_failed_error, :message =>AT_LEAST_ERROR_MESSAGE, :line => __LINE__+2) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once
end
end
end
def test_at_least_once_but_called_twice
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once
m.hi(1)
m.hi(1)
end
end
def test_at_least_and_exact
assert_mock_failure(check_failed_error, :message =>COUNT_ERROR_MESSAGE, :line => __LINE__+4) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once.once
m.hi(1)
m.hi(1)
end
end
end
def test_at_most_but_never_called
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_most.once
end
end
def test_at_most_called_once
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_most.once
m.hi(1)
end
end
def test_at_most_called_twice
ex = assert_mock_failure(check_failed_error, :message =>AT_MOST_ERROR_MESSAGE, :line => __LINE__+4) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_most.once
m.hi(1)
m.hi(1)
end
end
assert_match(/at most 1/i, ex.message)
end
def test_at_most_and_at_least_called_never
ex = assert_mock_failure(assertion_failed_error, :message =>AT_LEAST_ERROR_MESSAGE, :line => __LINE__+2) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
end
end
assert_match(/at least 1/i, ex.message)
end
def test_at_most_and_at_least_called_once
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
m.hi(1)
end
end
def test_at_most_and_at_least_called_twice
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
m.hi(1)
m.hi(1)
end
end
def test_at_most_and_at_least_called_three_times
assert_mock_failure(check_failed_error, :message =>AT_MOST_ERROR_MESSAGE, :line => __LINE__+5) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).returns(10).at_least.once.at_most.twice
m.hi(1)
m.hi(1)
m.hi(1)
end
end
end
def test_call_counts_only_apply_to_matching_args
FlexMock.use do |m|
m.should_receive(:hi).with(1).once
m.should_receive(:hi).with(2).twice
m.should_receive(:hi).with(3)
m.hi(1)
m.hi(2)
m.hi(2)
20.times { m.hi(3) }
end
end
def test_call_counts_only_apply_to_matching_args_with_mismatch
ex = assert_mock_failure(assertion_failed_error, :message =>COUNT_ERROR_MESSAGE, :line => __LINE__+3) do
FlexMock.use do |m|
m.should_receive(:hi).with(1).once
m.should_receive(:hi).with(2).twice
m.should_receive(:hi).with(3)
m.should_receive(:lo)
m.hi(1)
m.hi(2)
m.lo
20.times { m.hi(3) }
end
end
assert_match(/hi\(2\)/, ex.message)
end
def test_ordered_calls_in_order_will_pass
FlexMock.use 'm' do |m|
m.should_receive(:hi).ordered
m.should_receive(:lo).ordered
m.hi
m.lo
end
end
def test_ordered_calls_out_of_order_will_fail
assert_mock_failure(check_failed_error, :message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use 'm' do |m|
m.should_receive(:hi).ordered
m.should_receive(:lo).ordered
m.lo
m.hi
end
end
end
def test_failure_in_ordered_calls_combined_with_valid_count_will_report_an_order_failure
assert_mock_failure(check_failed_error, :message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use 'm', 'n' do |m, n|
m.should_receive(:hi).once.globally.ordered
n.should_receive(:lo).once.globally.ordered
n.lo
m.hi
end
end
end
def test_order_calls_with_different_arg_lists_and_in_order_will_pass
FlexMock.use 'm' do |m|
m.should_receive(:hi).with("one").ordered
m.should_receive(:hi).with("two").ordered
m.hi("one")
m.hi("two")
end
end
def test_order_calls_with_different_arg_lists_and_out_of_order_will_fail
assert_mock_failure(check_failed_error, :message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use 'm' do |m|
m.should_receive(:hi).with("one").ordered
m.should_receive(:hi).with("two").ordered
m.hi("two")
m.hi("one")
end
end
end
def test_unordered_calls_do_not_effect_ordered_testing
FlexMock.use 'm' do |m|
m.should_receive(:blah)
m.should_receive(:hi).ordered
m.should_receive(:lo).ordered
m.blah
m.hi
m.blah
m.lo
m.blah
end
end
def test_ordered_with_multiple_calls_will_pass
FlexMock.use 'm' do |m|
m.should_receive(:hi).ordered
m.should_receive(:lo).ordered
m.hi
m.hi
m.lo
m.lo
end
end
def test_grouped_ordering_with_numbers
FlexMock.use 'm' do |m|
m.should_receive(:start).ordered(1)
m.should_receive(:flip).ordered(2)
m.should_receive(:flop).ordered(2)
m.should_receive(:final).ordered
m.start
m.flop
m.flip
m.flop
m.final
end
end
def test_grouped_ordering_with_symbols
FlexMock.use 'm' do |m|
m.should_receive(:start).ordered(:start_group)
m.should_receive(:flip).ordered(:flip_flop_group)
m.should_receive(:flop).ordered(:flip_flop_group)
m.should_receive(:final).ordered
m.start
m.flop
m.flip
m.flop
m.final
end
end
def test_global_ordering_message_includes_received_calls
e = assert_mock_failure(check_failed_error, :message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use 'm' do |m|
m.should_receive(:one).globally.ordered
m.should_receive(:two).globally.ordered
m.one
m.two
m.one
end
end
assert_match(/one\(\) matched by should_receive\(:one\)/, e.message)
assert_match(/two\(\) matched by should_receive\(:two\)/, e.message)
assert_match(/one\(\) matched by should_receive\(:one\)/, e.message)
end
def test_ordering_message_includes_received_calls
e = assert_mock_failure(check_failed_error, :message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use 'm' do |m|
m.should_receive(:one).ordered
m.should_receive(:two).ordered
m.one
m.two
m.one
end
end
assert_match(/one\(\) matched by should_receive\(:one\)/, e.message)
assert_match(/two\(\) matched by should_receive\(:two\)/, e.message)
assert_match(/one\(\) matched by should_receive\(:one\)/, e.message)
end
def test_explicit_ordering_mixed_with_implicit_ordering_should_not_overlap
FlexMock.use 'm' do |m|
xstart = m.should_receive(:start).ordered
xmid = m.should_receive(:mid).ordered(:group_name)
xend = m.should_receive(:end).ordered
assert xstart.order_number < xmid.order_number
assert xmid.order_number < xend.order_number
end
end
def test_explicit_ordering_with_explicit_misorders
assert_mock_failure(check_failed_error, :message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use 'm' do |m|
m.should_receive(:hi).ordered(:first_group)
m.should_receive(:lo).ordered(:second_group)
m.lo
m.hi
end
end
# TODO: It would be nice to get the group names in the error message.
# assert_match /first_group/, ex.message
# assert_match /second_group/, ex.message
end
# Test submitted by Mikael Pahmp to correct expectation matching.
def test_ordering_with_explicit_no_args_matches_correctly
FlexMock.use("m") do |m|
m.should_receive(:foo).with_no_args.once.ordered
m.should_receive(:bar).with_no_args.once.ordered
m.should_receive(:foo).with_no_args.once.ordered
m.foo
m.bar
m.foo
end
end
# Test submitted by Mikael Pahmp to correct expectation matching.
def test_ordering_with_any_arg_matching_correctly_matches
FlexMock.use("m") do |m|
m.should_receive(:foo).with_any_args.once.ordered
m.should_receive(:bar).with_any_args.once.ordered
m.should_receive(:foo).with_any_args.once.ordered
m.foo
m.bar
m.foo
end
end
def test_ordering_between_mocks_is_not_normally_defined
FlexMock.use("x", "y") do |x, y|
x.should_receive(:one).ordered
y.should_receive(:two).ordered
y.two
x.one
end
end
def test_ordering_between_mocks_is_honored_for_global_ordering
assert_mock_failure(check_failed_error, :message =>OUT_OF_ORDER_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use("x", "y") do |x, y|
x.should_receive(:one).globally.ordered
y.should_receive(:two).globally.ordered
y.two
x.one
end
end
end
def test_ordering_is_verified_after_eligibility
assert_mock_failure(check_failed_error, :message =>COUNT_ERROR_MESSAGE, :deep => true, :line => __LINE__+6) do
FlexMock.use("x", "y") do |x, y|
x.should_receive(:one).once.ordered
x.should_receive(:two).ordered
x.one
x.two
x.one
end
end
end
def test_expectation_formating
mock = flexmock("m")
exp = mock.should_receive(:f).with(1,"two", /^3$/).
and_return(0).at_least.once
mock.f(1, "two", 3)
assert_equal 'f(1, "two", /^3$/)', exp.to_s
end
def test_multi_expectation_formatting
mock = flexmock("mock")
exp = mock.should_receive(:f, :g).with(1)
assert_equal "[f(1), g(1)]", exp.to_s
end
def test_explicit_ordering_with_limits_allow_multiple_return_values
FlexMock.use('mock') do |m|
m.should_receive(:f).with(2).once.and_return { :first_time }
m.should_receive(:f).with(2).twice.and_return { :second_or_third_time }
m.should_receive(:f).with(2).and_return { :forever }
assert_equal :first_time, m.f(2)
assert_equal :second_or_third_time, m.f(2)
assert_equal :second_or_third_time, m.f(2)
assert_equal :forever, m.f(2)
assert_equal :forever, m.f(2)
assert_equal :forever, m.f(2)
assert_equal :forever, m.f(2)
assert_equal :forever, m.f(2)
assert_equal :forever, m.f(2)
assert_equal :forever, m.f(2)
end
end
def test_global_methods_can_be_mocked
m = flexmock("m")
m.should_receive(:mock_top_level_function).and_return(:mock)
assert_equal :mock, m.mock_top_level_function
end
def test_kernel_methods_can_be_mocked
m = flexmock("m")
m.should_receive(:mock_kernel_function).and_return(:mock)
assert_equal :mock, m.mock_kernel_function
end
def test_undefing_kernel_methods_dont_effect_other_mocks
m = flexmock("m")
m2 = flexmock("m2")
m.should_receive(:mock_kernel_function).and_return(:mock)
assert_equal :mock, m.mock_kernel_function
assert_equal :mkf, m2.mock_kernel_function
end
def test_expectations_can_by_marked_as_default
m = flexmock("m")
m.should_receive(:foo).and_return(:bar).by_default
assert_equal :bar, m.foo
end
def test_default_expectations_are_search_in_the_proper_order
m = flexmock("m")
m.should_receive(:foo).with(Integer).once.and_return(:first).by_default
m.should_receive(:foo).with(1).and_return(:second).by_default
assert_equal :first, m.foo(1)
assert_equal :second, m.foo(1)
end
def test_expectations_with_count_constraints_can_by_marked_as_default
m = flexmock("m")
m.should_receive(:foo).and_return(:bar).once.by_default
assert_raises assertion_failed_error do
flexmock_teardown
end
end
def test_default_expectations_are_overridden_by_later_expectations
m = flexmock("m")
m.should_receive(:foo).and_return(:bar).once.by_default
m.should_receive(:foo).and_return(:bar).twice
m.foo
m.foo
end
def test_default_expectations_can_be_changed_by_later_expectations
m = flexmock("m")
m.should_receive(:foo).with(1).and_return(:bar).once.by_default
m.should_receive(:foo).with(2).and_return(:baz).once
assert_raises check_failed_error do
# This expectation should be hidded by the non-result
m.foo(1)
end
m.foo(2)
end
def test_ordered_default_expectations_can_be_specified
m = flexmock("m")
m.should_receive(:foo).ordered.by_default
m.should_receive(:bar).ordered.by_default
m.bar
assert_raises check_failed_error do m.foo end
end
def test_ordered_default_expectations_can_be_overridden
m = flexmock("m")
m.should_receive(:foo).ordered.by_default
m.should_receive(:bar).ordered.by_default
m.should_receive(:bar).ordered
m.should_receive(:foo).ordered
m.bar
m.foo
end
def test_by_default_works_at_mock_level
m = flexmock("m",
:foo => :bar,
:pooh => :bear,
:who => :dey).by_default
m.should_receive(:pooh => :winnie)
assert_equal :bar, m.foo
assert_equal :dey, m.who
assert_equal :winnie, m.pooh
end
def test_by_default_at_mock_level_does_nothing_with_no_expectations
flexmock("m").by_default
end
def test_partial_mocks_can_have_default_expectations
obj = Object.new
flexmock(obj).should_receive(:foo).and_return(:bar).by_default
assert_equal :bar, obj.foo
end
def test_partial_mocks_can_have_default_expectations_overridden
obj = Object.new
flexmock(obj).should_receive(:foo).and_return(:bar).by_default
flexmock(obj).should_receive(:foo).and_return(:baz)
assert_equal :baz, obj.foo
end
def test_wicked_and_evil_tricks_with_by_default_are_thwarted
mock = flexmock("mock")
exp = mock.should_receive(:foo).and_return(:first).once
mock.should_receive(:foo).and_return(:second)
ex = assert_raises(FlexMock::UsageError) do
exp.by_default
end
assert_match %r(previously defined), ex.message
assert_equal :first, mock.foo
assert_equal :second, mock.foo
end
def test_mocks_can_handle_multi_parameter_respond_tos
mock = flexmock("a mock", :foo => :bar)
assert mock.respond_to?(:foo)
assert mock.respond_to?(:foo, true)
assert mock.respond_to?(:foo, false)
assert ! mock.respond_to?(:phoo)
assert ! mock.respond_to?(:phoo, false)
assert ! mock.respond_to?(:phoo, true)
end
def test_backtraces_point_to_should_receive_line
mock = flexmock("a mock")
file_name_re = Regexp.quote(__FILE__)
line_no = __LINE__ + 1
mock.should_receive(:foo).and_return(:bar).once
begin
flexmock_verify
rescue Exception => ex
exception = ex
end
refute_nil exception
assert_match(/#{file_name_re}:#{line_no}/, exception.backtrace.first)
end
def test_can_mock_operators
assert_operator(:[]) { |m| m[1] }
assert_operator(:[]=) { |m| m[1] = :value }
assert_operator(:**) { |m| m ** :x }
assert_operator(:+@) { |m| +m }
assert_operator(:-@) { |m| -m }
assert_operator(:+) { |m| m + :x }
assert_operator(:-) { |m| m - :x }
assert_operator(:*) { |m| m * :x }
assert_operator(:"/") { |m| m / :x }
assert_operator(:%) { |m| m % :x }
assert_operator(:~) { |m| ~m } # )
assert_operator(:&) { |m| m & :x }
assert_operator(:|) { |m| m | :x }
assert_operator(:^) { |m| m ^ :x }
assert_operator(:<) { |m| m < :x }
assert_operator(:>) { |m| m > :x }
assert_operator(:>=) { |m| m >= :x }
assert_operator(:<=) { |m| m <= :x }
assert_operator(:==) { |m| m == :x }
assert_operator(:===) { |m| m === :x }
assert_operator(:<<) { |m| m << :x }
assert_operator(:>>) { |m| m >> :x }
assert_operator(:<=>) { |m| m <=> :x }
assert_operator(:=~) { |m| m =~ :x }
assert_operator(:"`") { |m| m.`("command") } # `
end
def test_with_signature_required_arguments
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 2)
assert_mock_failure(check_failed_error, message: /expects at least 2 positional arguments but got only 1/, line: __LINE__+1) do
mock.m(1)
end
mock.m(1, 2)
assert_mock_failure(check_failed_error, message: /expects at most 2 positional arguments but got 3/, line: __LINE__+1) do
mock.m(1, 2, 3)
end
end
end
def test_a_proc_argument_last_is_not_interpreted_as_positional_argument
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 2)
mock.m(1, 2) { }
assert_raises(FlexMock::CheckFailedError) do
mock.m(1) { }
end
end
end
def test_with_signature_optional_arguments
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 2, optional_arguments: 2)
assert_mock_failure(check_failed_error, message: /expects at least 2 positional arguments but got only 1/, line: __LINE__+1) do
mock.m(1)
end
mock.m(1, 2)
mock.m(1, 2, 3)
mock.m(1, 2, 3, 4)
assert_mock_failure(check_failed_error, message: /expects at most 4 positional arguments but got 5/, line: __LINE__+1) do
mock.m(1, 2, 3, 4, 5)
end
end
end
def test_with_signature_splat_validates_required_arguments
FlexMock.use do |mock|
mock = flexmock
mock.should_receive(:m).with_signature(required_arguments: 2, optional_arguments: 2, splat: true)
assert_mock_failure(check_failed_error, message: /expects at least 2 positional arguments but got only 1/, line: __LINE__+1) do
mock.m(1)
end
end
end
def test_with_signature_splat_allows_an_arbitrary_number_of_extra_arguments
FlexMock.use do |mock|
mock = flexmock
mock.should_receive(:m).with_signature(required_arguments: 2, optional_arguments: 2, splat: true)
mock.m(1, 2)
mock.m(1, 2, 3)
mock.m(1, 2, 3, 4)
end
end
def test_with_signature_required_keyword_arguments
FlexMock.use do |mock|
mock = flexmock
mock.should_receive(:m).
with_signature(required_keyword_arguments: [:a, :b])
assert_mock_failure(check_failed_error, message: /missing required keyword arguments a/, line: __LINE__+1) do
mock.m(b: 10)
end
mock.m(a: 10, b: 20)
assert_mock_failure(check_failed_error, message: /given unexpected keyword argument c/, line: __LINE__+1) do
mock.m(a: 10, b: 10, c: 20)
end
end
end
def test_with_signature_optional_keyword_arguments
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(required_keyword_arguments: [:a, :b], optional_keyword_arguments: [:c, :d])
assert_mock_failure(check_failed_error, message: /missing required keyword arguments a/, line: __LINE__+1) do
mock.m(b: 10)
end
mock.m(a: 10, b: 20)
mock.m(a: 10, b: 20, c: 30)
mock.m(a: 10, b: 20, c: 30, d: 40)
assert_mock_failure(check_failed_error, message: /given unexpected keyword argument e/, line: __LINE__+1) do
mock.m(a: 10, b: 10, e: 42)
end
end
end
def test_with_signature_keyword_splat
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(
required_keyword_arguments: [:a, :b],
optional_keyword_arguments: [:c, :d],
keyword_splat: true)
assert_mock_failure(check_failed_error, message: /missing required keyword arguments a/, line: __LINE__+1) do
mock.m(b: 10)
end
mock.m(a: 10, b: 20)
mock.m(a: 10, b: 20, c: 30)
mock.m(a: 10, b: 20, c: 30, d: 40)
mock.m(a: 10, b: 10, e: 42)
end
end
def test_with_signature_raises_if_no_keywords_are_given
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(required_keyword_arguments: [:b], required_arguments: 1)
assert_mock_failure(check_failed_error, message: /expects keyword arguments but none were provided/, line: __LINE__+1) do
mock.m(10)
end
end
end
def test_with_signature_handles_getting_a_basicobject_as_last_object
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(optional_arguments: 1, required_keyword_arguments: [:b])
assert_mock_failure(check_failed_error, message: /expects keyword arguments but none were provided/, line: __LINE__+1) do
mock.m(BasicObject.new)
end
end
end
def test_with_signature_removes_the_keywords_from_the_position_arguments
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(required_keyword_arguments: [:b], required_arguments: 1)
mock.m(10, b: 10)
end
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(optional_keyword_arguments: [:b], required_arguments: 1)
mock.m(10, b: 10)
end
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(keyword_splat: true, required_arguments: 1)
mock.m(10, b: 10)
end
end
def test_signature_validator_understands_that_a_proc_last_can_both_be_a_positional_parameter_and_a_block
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 1)
mock.m(proc { })
end
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 0)
mock.m { }
end
end
def test_signature_validator_interprets_keyword_arguments_even_if_a_block_is_provided
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 1, optional_keyword_arguments: [:b])
mock.m(10, b: 10) { }
end
end
def test_signature_validator_does_not_interpret_a_proc_as_positional_argument_if_keyword_arguments_are_expected
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 1, required_keyword_arguments: [:b])
mock.m(10, b: 10) { }
signature = if RUBY_VERSION < "3"
'm\(\*args\)'
else
'm\(\*args, \*\*kwargs\)'
end
assert_mock_failure(check_failed_error, message: /in mock 'unknown': #{signature} expects at least 1 positional arguments but got only 0/, line: __LINE__+1) do
mock.m(b: 10) { }
end
end
end
def test_signature_validator_does_accept_both_a_hash_and_a_proc_as_positional_arguments
FlexMock.use do |mock|
mock.should_receive(:m).with_signature(required_arguments: 3)
mock.m(10, Hash[b: 10], proc {})
end
end
def test_signature_validator_does_not_accept_a_lone_hash_as_positional_argument_if_there_are_required_keyword_arguments
FlexMock.use do |mock|
mock.should_receive(:m).
with_signature(required_keyword_arguments: [:b], required_arguments: 1)
signature = 'm\(\*args, \*\*kwargs\)'
if RUBY_VERSION < "3"
signature = 'm\(\*args\)'
assert_mock_failure(check_failed_error, message: /in mock 'unknown': #{signature} expects at least 1 positional arguments but got only 0/, line: __LINE__+1) do
mock.m({ :foo => "bar" })
end
end
assert_mock_failure(check_failed_error, message: /in mock 'unknown': #{signature} expects at least 1 positional arguments but got only 0/, line: __LINE__+1) do
mock.m(b: 10)
end
end
end
def test_with_signature_matching_sets_up_the_signature_predicate_based_on_the_provided_instance_method_positional_arguments
k = Class.new { def m(req_a, req_b, opt_c = 10); end }
FlexMock.use do |mock|
e = mock.should_receive(:m).with_signature_matching(k.instance_method(:m))
e = e.instance_variable_get(:@expectations).first
validator = e.instance_variable_get(:@signature_validator)
assert_equal 2, validator.required_arguments
assert_equal 1, validator.optional_arguments
refute validator.splat?
assert_equal Set.new, validator.required_keyword_arguments
assert_equal Set.new, validator.optional_keyword_arguments
assert_equal false, validator.keyword_splat?
end
end
def test_with_signature_matching_sets_up_the_signature_predicate_based_on_the_provided_instance_method_splat
k = Class.new { def m(req_a, req_b, opt_c = 10, *splat); end }
FlexMock.use do |mock|
e = mock.should_receive(:m).with_signature_matching(k.instance_method(:m))
e = e.instance_variable_get(:@expectations).first
validator = e.instance_variable_get(:@signature_validator)
assert_equal 2, validator.required_arguments
assert_equal 1, validator.optional_arguments
assert validator.splat?
assert_equal Set.new, validator.required_keyword_arguments
assert_equal Set.new, validator.optional_keyword_arguments
assert_equal false, validator.keyword_splat?
end
end
def test_signature_validator_from_instance_method_raises_if_the_method_description_contains_an_unknown_argument_type
mock = flexmock(parameters: [[:unknown]])
error = assert_raises(ArgumentError) do
FlexMock::SignatureValidator.from_instance_method(flexmock, mock)
end
assert_equal "cannot interpret parameter type unknown", error.message
end
def test_private_Object_methods_can_be_mocked
FlexMock.use do |m|
m.should_receive(:warn).returns(1)
assert_equal 1, m.warn
assert_equal 1, m.send(:warn)
end
end
private
def assert_operator(op, &block)
m = flexmock("mock")
m.should_receive(op).and_return(:value)
assert_equal :value, block.call(m)
end
end
class TestFlexMockShouldsWithInclude < Minitest::Test
include FlexMock::ArgumentTypes
def test_include_enables_unqualified_arg_type_references
FlexMock.use("x") do |m|
m.should_receive(:hi).with(any).once
m.hi(1)
end
end
end
class TestFlexMockArgTypesDontLeak < Minitest::Test
def test_unqualified_arg_type_references_are_undefined_by_default
ex = assert_raises(NameError) do
FlexMock.use("x") do |m|
m.should_receive(:hi).with(any).once
m.hi(1)
end
end
assert_match(/\bany\b/, ex.message, "Error message should mention 'any'")
end
end
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.1')
require_relative 'should_receive_ruby21plus'
end
|