PageRenderTime 142ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 1ms

/Languages/Ruby/Tests/mspec/mspec/spec/runner/context_spec.rb

http://github.com/IronLanguages/main
Ruby | 1014 lines | 845 code | 169 blank | 0 comment | 94 complexity | 07db1f9a34f5a3fe7b9da38f8f6a59f7 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require File.dirname(__FILE__) + '/../spec_helper'
  2. require 'mspec/expectations/expectations'
  3. require 'mspec/matchers/base'
  4. require 'mspec/runner/mspec'
  5. require 'mspec/mocks/mock'
  6. require 'mspec/runner/context'
  7. require 'mspec/runner/example'
  8. describe ContextState, "#describe" do
  9. before :each do
  10. @state = ContextState.new "C#m"
  11. @proc = lambda { ScratchPad.record :a }
  12. ScratchPad.clear
  13. end
  14. it "evaluates the passed block" do
  15. @state.describe(&@proc)
  16. ScratchPad.recorded.should == :a
  17. end
  18. it "evaluates the passed block via #protect" do
  19. @state.should_receive(:protect).with("C#m", @proc, false)
  20. @state.describe(&@proc)
  21. end
  22. it "registers #parent as the current MSpec ContextState" do
  23. parent = ContextState.new ""
  24. @state.parent = parent
  25. MSpec.should_receive(:register_current).with(parent)
  26. @state.describe { }
  27. end
  28. it "registers self with MSpec when #shared? is true" do
  29. state = ContextState.new "something shared", :shared => true
  30. MSpec.should_receive(:register_shared).with(state)
  31. state.describe { }
  32. end
  33. end
  34. describe ContextState, "#shared?" do
  35. it "returns false when the ContextState is not shared" do
  36. ContextState.new("").shared?.should be_false
  37. end
  38. it "returns true when the ContextState is shared" do
  39. ContextState.new("", {:shared => true}).shared?.should be_true
  40. end
  41. end
  42. describe ContextState, "#to_s" do
  43. it "returns a description string for self when passed a Module" do
  44. ContextState.new(Object).to_s.should == "Object"
  45. end
  46. it "returns a description string for self when passed a String" do
  47. ContextState.new("SomeClass").to_s.should == "SomeClass"
  48. end
  49. it "returns a description string for self when passed a Module, String" do
  50. ContextState.new(Object, "when empty").to_s.should == "Object when empty"
  51. end
  52. it "returns a description string for self when passed a Module and String beginning with '#'" do
  53. ContextState.new(Object, "#to_s").to_s.should == "Object#to_s"
  54. end
  55. it "returns a description string for self when passed a Module and String beginning with '.'" do
  56. ContextState.new(Object, ".to_s").to_s.should == "Object.to_s"
  57. end
  58. it "returns a description string for self when passed a Module and String beginning with '::'" do
  59. ContextState.new(Object, "::to_s").to_s.should == "Object::to_s"
  60. end
  61. end
  62. describe ContextState, "#description" do
  63. before :each do
  64. @state = ContextState.new "when empty"
  65. @parent = ContextState.new "Toplevel"
  66. end
  67. it "returns a composite description string from self and all parents" do
  68. @parent.description.should == "Toplevel"
  69. @state.description.should == "when empty"
  70. @state.parent = @parent
  71. @state.description.should == "Toplevel when empty"
  72. end
  73. end
  74. describe ContextState, "#it" do
  75. before :each do
  76. @state = ContextState.new ""
  77. @proc = lambda { }
  78. @ex = ExampleState.new("", "", &@proc)
  79. end
  80. it "creates an ExampleState instance for the block" do
  81. ExampleState.should_receive(:new).with(@state, "it", @proc).and_return(@ex)
  82. @state.describe(&@proc)
  83. @state.it("it", &@proc)
  84. end
  85. it "calls registered :add actions" do
  86. ExampleState.should_receive(:new).with(@state, "it", @proc).and_return(@ex)
  87. add_action = mock("add")
  88. add_action.should_receive(:add).with(@ex).and_return { ScratchPad.record :add }
  89. MSpec.register :add, add_action
  90. @state.it("it", &@proc)
  91. ScratchPad.recorded.should == :add
  92. MSpec.unregister :add, add_action
  93. end
  94. end
  95. describe ContextState, "#examples" do
  96. before :each do
  97. @state = ContextState.new ""
  98. end
  99. it "returns a list of all examples in this ContextState" do
  100. @state.it("first") { }
  101. @state.it("second") { }
  102. @state.examples.size.should == 2
  103. end
  104. end
  105. describe ContextState, "#before" do
  106. before :each do
  107. @state = ContextState.new ""
  108. @proc = lambda { }
  109. end
  110. it "records the block for :each" do
  111. @state.before(:each, &@proc)
  112. @state.before(:each).should == [@proc]
  113. end
  114. it "records the block for :all" do
  115. @state.before(:all, &@proc)
  116. @state.before(:all).should == [@proc]
  117. end
  118. end
  119. describe ContextState, "#after" do
  120. before :each do
  121. @state = ContextState.new ""
  122. @proc = lambda { }
  123. end
  124. it "records the block for :each" do
  125. @state.after(:each, &@proc)
  126. @state.after(:each).should == [@proc]
  127. end
  128. it "records the block for :all" do
  129. @state.after(:all, &@proc)
  130. @state.after(:all).should == [@proc]
  131. end
  132. end
  133. describe ContextState, "#pre" do
  134. before :each do
  135. @a = lambda { }
  136. @b = lambda { }
  137. @c = lambda { }
  138. parent = ContextState.new ""
  139. parent.before(:each, &@c)
  140. parent.before(:all, &@c)
  141. @state = ContextState.new ""
  142. @state.parent = parent
  143. end
  144. it "returns before(:each) actions in the order they were defined" do
  145. @state.before(:each, &@a)
  146. @state.before(:each, &@b)
  147. @state.pre(:each).should == [@c, @a, @b]
  148. end
  149. it "returns before(:all) actions in the order they were defined" do
  150. @state.before(:all, &@a)
  151. @state.before(:all, &@b)
  152. @state.pre(:all).should == [@c, @a, @b]
  153. end
  154. end
  155. describe ContextState, "#post" do
  156. before :each do
  157. @a = lambda { }
  158. @b = lambda { }
  159. @c = lambda { }
  160. parent = ContextState.new ""
  161. parent.after(:each, &@c)
  162. parent.after(:all, &@c)
  163. @state = ContextState.new ""
  164. @state.parent = parent
  165. end
  166. it "returns after(:each) actions in the reverse order they were defined" do
  167. @state.after(:each, &@a)
  168. @state.after(:each, &@b)
  169. @state.post(:each).should == [@b, @a, @c]
  170. end
  171. it "returns after(:all) actions in the reverse order they were defined" do
  172. @state.after(:all, &@a)
  173. @state.after(:all, &@b)
  174. @state.post(:all).should == [@b, @a, @c]
  175. end
  176. end
  177. describe ContextState, "#protect" do
  178. before :each do
  179. ScratchPad.record []
  180. @a = lambda { ScratchPad << :a }
  181. @b = lambda { ScratchPad << :b }
  182. @c = lambda { raise Exception, "Fail!" }
  183. end
  184. it "returns true and does execute any blocks if check and MSpec.mode?(:pretend) are true" do
  185. MSpec.should_receive(:mode?).with(:pretend).and_return(true)
  186. ContextState.new("").protect("message", [@a, @b]).should be_true
  187. ScratchPad.recorded.should == []
  188. end
  189. it "executes the blocks if MSpec.mode?(:pretend) is false" do
  190. MSpec.should_receive(:mode?).with(:pretend).and_return(false)
  191. ContextState.new("").protect("message", [@a, @b])
  192. ScratchPad.recorded.should == [:a, :b]
  193. end
  194. it "executes the blocks if check is false" do
  195. ContextState.new("").protect("message", [@a, @b], false)
  196. ScratchPad.recorded.should == [:a, :b]
  197. end
  198. it "returns true if none of the blocks raise an exception" do
  199. ContextState.new("").protect("message", [@a, @b]).should be_true
  200. end
  201. it "returns false if any of the blocks raise an exception" do
  202. ContextState.new("").protect("message", [@a, @c, @b]).should be_false
  203. end
  204. end
  205. describe ContextState, "#parent=" do
  206. before :each do
  207. @state = ContextState.new ""
  208. @parent = mock("describe")
  209. @parent.stub!(:parent).and_return(nil)
  210. @parent.stub!(:child)
  211. end
  212. it "does not set self as a child of parent if shared" do
  213. @parent.should_not_receive(:child)
  214. state = ContextState.new "", :shared => true
  215. state.parent = @parent
  216. end
  217. it "sets self as a child of parent" do
  218. @parent.should_receive(:child).with(@state)
  219. @state.parent = @parent
  220. end
  221. it "creates the list of parents" do
  222. @state.parent = @parent
  223. @state.parents.should == [@parent, @state]
  224. end
  225. end
  226. describe ContextState, "#parent" do
  227. before :each do
  228. @state = ContextState.new ""
  229. @parent = mock("describe")
  230. @parent.stub!(:parent).and_return(nil)
  231. @parent.stub!(:child)
  232. end
  233. it "returns nil if parent has not been set" do
  234. @state.parent.should be_nil
  235. end
  236. it "returns the parent" do
  237. @state.parent = @parent
  238. @state.parent.should == @parent
  239. end
  240. end
  241. describe ContextState, "#parents" do
  242. before :each do
  243. @first = ContextState.new ""
  244. @second = ContextState.new ""
  245. @parent = mock("describe")
  246. @parent.stub!(:parent).and_return(nil)
  247. @parent.stub!(:child)
  248. end
  249. it "returns a list of all enclosing ContextState instances" do
  250. @first.parent = @parent
  251. @second.parent = @first
  252. @second.parents.should == [@parent, @first, @second]
  253. end
  254. end
  255. describe ContextState, "#child" do
  256. before :each do
  257. @first = ContextState.new ""
  258. @second = ContextState.new ""
  259. @parent = mock("describe")
  260. @parent.stub!(:parent).and_return(nil)
  261. @parent.stub!(:child)
  262. end
  263. it "adds the ContextState to the list of contained ContextStates" do
  264. @first.child @second
  265. @first.children.should == [@second]
  266. end
  267. end
  268. describe ContextState, "#children" do
  269. before :each do
  270. @parent = ContextState.new ""
  271. @first = ContextState.new ""
  272. @second = ContextState.new ""
  273. end
  274. it "returns the list of directly contained ContextStates" do
  275. @first.parent = @parent
  276. @second.parent = @first
  277. @parent.children.should == [@first]
  278. @first.children.should == [@second]
  279. end
  280. end
  281. describe ContextState, "#state" do
  282. before :each do
  283. MSpec.store :before, []
  284. MSpec.store :after, []
  285. @state = ContextState.new ""
  286. end
  287. it "returns nil if no spec is being executed" do
  288. @state.state.should == nil
  289. end
  290. it "returns a ExampleState instance if an example is being executed" do
  291. ScratchPad.record @state
  292. @state.describe { }
  293. @state.it("") { ScratchPad.record ScratchPad.recorded.state }
  294. @state.process
  295. @state.state.should == nil
  296. ScratchPad.recorded.should be_kind_of(ExampleState)
  297. end
  298. end
  299. describe ContextState, "#process" do
  300. before :each do
  301. MSpec.store :before, []
  302. MSpec.store :after, []
  303. MSpec.stub!(:register_current)
  304. @state = ContextState.new ""
  305. @state.describe { }
  306. @a = lambda { ScratchPad << :a }
  307. @b = lambda { ScratchPad << :b }
  308. ScratchPad.record []
  309. end
  310. it "calls each before(:all) block" do
  311. @state.before(:all, &@a)
  312. @state.before(:all, &@b)
  313. @state.it("") { }
  314. @state.process
  315. ScratchPad.recorded.should == [:a, :b]
  316. end
  317. it "calls each after(:all) block" do
  318. @state.after(:all, &@a)
  319. @state.after(:all, &@b)
  320. @state.it("") { }
  321. @state.process
  322. ScratchPad.recorded.should == [:b, :a]
  323. end
  324. it "calls each it block" do
  325. @state.it("one", &@a)
  326. @state.it("two", &@b)
  327. @state.process
  328. ScratchPad.recorded.should == [:a, :b]
  329. end
  330. it "does not call the #it block if #filtered? returns true" do
  331. @state.it("one", &@a)
  332. @state.it("two", &@b)
  333. @state.examples.first.stub!(:filtered?).and_return(true)
  334. @state.process
  335. ScratchPad.recorded.should == [:b]
  336. end
  337. it "calls each before(:each) block" do
  338. @state.before(:each, &@a)
  339. @state.before(:each, &@b)
  340. @state.it("") { }
  341. @state.process
  342. ScratchPad.recorded.should == [:a, :b]
  343. end
  344. it "calls each after(:each) block" do
  345. @state.after(:each, &@a)
  346. @state.after(:each, &@b)
  347. @state.it("") { }
  348. @state.process
  349. ScratchPad.recorded.should == [:b, :a]
  350. end
  351. it "calls Mock.cleanup for each it block" do
  352. @state.it("") { }
  353. @state.it("") { }
  354. Mock.should_receive(:cleanup).twice
  355. @state.process
  356. end
  357. it "calls Mock.verify_count for each it block" do
  358. @state.it("") { }
  359. @state.it("") { }
  360. Mock.should_receive(:verify_count).twice
  361. @state.process
  362. end
  363. it "calls the describe block" do
  364. ScratchPad.record []
  365. @state.describe { ScratchPad << :a }
  366. @state.process
  367. ScratchPad.recorded.should == [:a]
  368. end
  369. it "creates a new ExampleState instance for each example" do
  370. ScratchPad.record @state
  371. @state.describe { }
  372. @state.it("it") { ScratchPad.record ScratchPad.recorded.state }
  373. @state.process
  374. ScratchPad.recorded.should be_kind_of(ExampleState)
  375. end
  376. it "clears the expectations flag before evaluating the #it block" do
  377. MSpec.clear_expectations
  378. MSpec.should_receive(:clear_expectations)
  379. @state.it("it") { ScratchPad.record MSpec.expectation? }
  380. @state.process
  381. ScratchPad.recorded.should be_false
  382. end
  383. it "shuffles the spec list if MSpec.randomize? is true" do
  384. MSpec.randomize
  385. MSpec.should_receive(:shuffle)
  386. @state.it("") { }
  387. @state.process
  388. MSpec.randomize false
  389. end
  390. it "sets the current MSpec ContextState" do
  391. MSpec.should_receive(:register_current).with(@state)
  392. @state.process
  393. end
  394. it "resets the current MSpec ContextState to nil when there are examples" do
  395. MSpec.should_receive(:register_current).with(nil)
  396. @state.it("") { }
  397. @state.process
  398. end
  399. it "resets the current MSpec ContextState to nil when there are no examples" do
  400. MSpec.should_receive(:register_current).with(nil)
  401. @state.process
  402. end
  403. it "call #process on children when there are examples" do
  404. child = ContextState.new ""
  405. child.should_receive(:process)
  406. @state.child child
  407. @state.it("") { }
  408. @state.process
  409. end
  410. it "call #process on children when there are no examples" do
  411. child = ContextState.new ""
  412. child.should_receive(:process)
  413. @state.child child
  414. @state.process
  415. end
  416. end
  417. describe ContextState, "#process" do
  418. before :each do
  419. MSpec.store :exception, []
  420. @state = ContextState.new ""
  421. @state.describe { }
  422. action = mock("action")
  423. def action.exception(exc)
  424. ScratchPad.record :exception if exc.exception.is_a? SpecExpectationNotFoundError
  425. end
  426. MSpec.register :exception, action
  427. MSpec.clear_expectations
  428. ScratchPad.clear
  429. end
  430. after :each do
  431. MSpec.store :exception, nil
  432. end
  433. it "raises an SpecExpectationNotFoundError if an #it block does not contain an expectation" do
  434. @state.it("it") { }
  435. @state.process
  436. ScratchPad.recorded.should == :exception
  437. end
  438. it "does not raise an SpecExpectationNotFoundError if an #it block does contain an expectation" do
  439. @state.it("it") { MSpec.expectation }
  440. @state.process
  441. ScratchPad.recorded.should be_nil
  442. end
  443. it "does not raise an SpecExpectationNotFoundError if the #it block causes a failure" do
  444. @state.it("it") { raise Exception, "Failed!" }
  445. @state.process
  446. ScratchPad.recorded.should be_nil
  447. end
  448. end
  449. describe ContextState, "#process" do
  450. before :each do
  451. MSpec.store :example, []
  452. @state = ContextState.new ""
  453. @state.describe { }
  454. example = mock("example")
  455. def example.example(state, spec)
  456. ScratchPad << state << spec
  457. end
  458. MSpec.register :example, example
  459. ScratchPad.record []
  460. end
  461. after :each do
  462. MSpec.store :example, nil
  463. end
  464. it "calls registered :example actions with the current ExampleState and block" do
  465. @state.it("") { MSpec.expectation }
  466. @state.process
  467. ScratchPad.recorded.first.should be_kind_of(ExampleState)
  468. ScratchPad.recorded.last.should be_kind_of(Proc)
  469. end
  470. it "does not call registered example actions if the example has no block" do
  471. @state.it("empty example")
  472. @state.process
  473. ScratchPad.recorded.should == []
  474. end
  475. end
  476. describe ContextState, "#process" do
  477. before :each do
  478. MSpec.store :before, []
  479. MSpec.store :after, []
  480. @state = ContextState.new ""
  481. @state.describe { }
  482. @state.it("") { MSpec.expectation }
  483. end
  484. after :each do
  485. MSpec.store :before, nil
  486. MSpec.store :after, nil
  487. end
  488. it "calls registered :before actions with the current ExampleState instance" do
  489. before = mock("before")
  490. before.should_receive(:before).and_return {
  491. ScratchPad.record :before
  492. @spec_state = @state.state
  493. }
  494. MSpec.register :before, before
  495. @state.process
  496. ScratchPad.recorded.should == :before
  497. @spec_state.should be_kind_of(ExampleState)
  498. end
  499. it "calls registered :after actions with the current ExampleState instance" do
  500. after = mock("after")
  501. after.should_receive(:after).and_return {
  502. ScratchPad.record :after
  503. @spec_state = @state.state
  504. }
  505. MSpec.register :after, after
  506. @state.process
  507. ScratchPad.recorded.should == :after
  508. @spec_state.should be_kind_of(ExampleState)
  509. end
  510. end
  511. describe ContextState, "#process" do
  512. before :each do
  513. MSpec.store :enter, []
  514. MSpec.store :leave, []
  515. @state = ContextState.new "C#m"
  516. @state.describe { }
  517. @state.it("") { MSpec.expectation }
  518. end
  519. after :each do
  520. MSpec.store :enter, nil
  521. MSpec.store :leave, nil
  522. end
  523. it "calls registered :enter actions with the current #describe string" do
  524. enter = mock("enter")
  525. enter.should_receive(:enter).with("C#m").and_return { ScratchPad.record :enter }
  526. MSpec.register :enter, enter
  527. @state.process
  528. ScratchPad.recorded.should == :enter
  529. end
  530. it "calls registered :leave actions" do
  531. leave = mock("leave")
  532. leave.should_receive(:leave).and_return { ScratchPad.record :leave }
  533. MSpec.register :leave, leave
  534. @state.process
  535. ScratchPad.recorded.should == :leave
  536. end
  537. end
  538. describe ContextState, "#process when an exception is raised in before(:all)" do
  539. before :each do
  540. MSpec.store :before, []
  541. MSpec.store :after, []
  542. @state = ContextState.new ""
  543. @state.describe { }
  544. @a = lambda { ScratchPad << :a }
  545. @b = lambda { ScratchPad << :b }
  546. ScratchPad.record []
  547. @state.before(:all) { raise Exception, "Fail!" }
  548. end
  549. after :each do
  550. MSpec.store :before, nil
  551. MSpec.store :after, nil
  552. end
  553. it "does not call before(:each)" do
  554. @state.before(:each, &@a)
  555. @state.it("") { }
  556. @state.process
  557. ScratchPad.recorded.should == []
  558. end
  559. it "does not call the it block" do
  560. @state.it("one", &@a)
  561. @state.process
  562. ScratchPad.recorded.should == []
  563. end
  564. it "does not call after(:each)" do
  565. @state.after(:each, &@a)
  566. @state.it("") { }
  567. @state.process
  568. ScratchPad.recorded.should == []
  569. end
  570. it "does not call after(:each)" do
  571. @state.after(:all, &@a)
  572. @state.it("") { }
  573. @state.process
  574. ScratchPad.recorded.should == []
  575. end
  576. it "does not call Mock.verify_count" do
  577. @state.it("") { }
  578. Mock.should_not_receive(:verify_count)
  579. @state.process
  580. end
  581. it "calls Mock.cleanup" do
  582. @state.it("") { }
  583. Mock.should_receive(:cleanup)
  584. @state.process
  585. end
  586. end
  587. describe ContextState, "#process when an exception is raised in before(:each)" do
  588. before :each do
  589. MSpec.store :before, []
  590. MSpec.store :after, []
  591. @state = ContextState.new ""
  592. @state.describe { }
  593. @a = lambda { ScratchPad << :a }
  594. @b = lambda { ScratchPad << :b }
  595. ScratchPad.record []
  596. @state.before(:each) { raise Exception, "Fail!" }
  597. end
  598. after :each do
  599. MSpec.store :before, nil
  600. MSpec.store :after, nil
  601. end
  602. it "does not call the it block" do
  603. @state.it("one", &@a)
  604. @state.process
  605. ScratchPad.recorded.should == []
  606. end
  607. it "does not call after(:each)" do
  608. @state.after(:each, &@a)
  609. @state.it("") { }
  610. @state.process
  611. ScratchPad.recorded.should == []
  612. end
  613. it "does not call Mock.verify_count" do
  614. @state.it("") { }
  615. Mock.should_not_receive(:verify_count)
  616. @state.process
  617. end
  618. end
  619. describe ContextState, "#process in pretend mode" do
  620. before :all do
  621. MSpec.register_mode :pretend
  622. end
  623. after :all do
  624. MSpec.register_mode nil
  625. end
  626. before :each do
  627. ScratchPad.clear
  628. MSpec.store :before, []
  629. MSpec.store :after, []
  630. @state = ContextState.new ""
  631. @state.describe { }
  632. @state.it("") { }
  633. end
  634. after :each do
  635. MSpec.store :before, nil
  636. MSpec.store :after, nil
  637. end
  638. it "calls registered :before actions with the current ExampleState instance" do
  639. before = mock("before")
  640. before.should_receive(:before).and_return {
  641. ScratchPad.record :before
  642. @spec_state = @state.state
  643. }
  644. MSpec.register :before, before
  645. @state.process
  646. ScratchPad.recorded.should == :before
  647. @spec_state.should be_kind_of(ExampleState)
  648. end
  649. it "calls registered :after actions with the current ExampleState instance" do
  650. after = mock("after")
  651. after.should_receive(:after).and_return {
  652. ScratchPad.record :after
  653. @spec_state = @state.state
  654. }
  655. MSpec.register :after, after
  656. @state.process
  657. ScratchPad.recorded.should == :after
  658. @spec_state.should be_kind_of(ExampleState)
  659. end
  660. end
  661. describe ContextState, "#process in pretend mode" do
  662. before :all do
  663. MSpec.register_mode :pretend
  664. end
  665. after :all do
  666. MSpec.register_mode nil
  667. end
  668. before :each do
  669. MSpec.store :before, []
  670. MSpec.store :after, []
  671. @state = ContextState.new ""
  672. @state.describe { }
  673. @a = lambda { ScratchPad << :a }
  674. @b = lambda { ScratchPad << :b }
  675. ScratchPad.record []
  676. end
  677. it "calls the describe block" do
  678. ScratchPad.record []
  679. @state.describe { ScratchPad << :a }
  680. @state.process
  681. ScratchPad.recorded.should == [:a]
  682. end
  683. it "does not call any before(:all) block" do
  684. @state.before(:all, &@a)
  685. @state.before(:all, &@b)
  686. @state.it("") { }
  687. @state.process
  688. ScratchPad.recorded.should == []
  689. end
  690. it "does not call any after(:all) block" do
  691. @state.after(:all, &@a)
  692. @state.after(:all, &@b)
  693. @state.it("") { }
  694. @state.process
  695. ScratchPad.recorded.should == []
  696. end
  697. it "does not call any it block" do
  698. @state.it("one", &@a)
  699. @state.it("two", &@b)
  700. @state.process
  701. ScratchPad.recorded.should == []
  702. end
  703. it "does not call any before(:each) block" do
  704. @state.before(:each, &@a)
  705. @state.before(:each, &@b)
  706. @state.it("") { }
  707. @state.process
  708. ScratchPad.recorded.should == []
  709. end
  710. it "does not call any after(:each) block" do
  711. @state.after(:each, &@a)
  712. @state.after(:each, &@b)
  713. @state.it("") { }
  714. @state.process
  715. ScratchPad.recorded.should == []
  716. end
  717. it "does not call Mock.cleanup" do
  718. @state.it("") { }
  719. @state.it("") { }
  720. Mock.should_not_receive(:cleanup)
  721. @state.process
  722. end
  723. end
  724. describe ContextState, "#process in pretend mode" do
  725. before :all do
  726. MSpec.register_mode :pretend
  727. end
  728. after :all do
  729. MSpec.register_mode nil
  730. end
  731. before :each do
  732. MSpec.store :enter, []
  733. MSpec.store :leave, []
  734. @state = ContextState.new ""
  735. @state.describe { }
  736. @state.it("") { }
  737. end
  738. after :each do
  739. MSpec.store :enter, nil
  740. MSpec.store :leave, nil
  741. end
  742. it "calls registered :enter actions with the current #describe string" do
  743. enter = mock("enter")
  744. enter.should_receive(:enter).and_return { ScratchPad.record :enter }
  745. MSpec.register :enter, enter
  746. @state.process
  747. ScratchPad.recorded.should == :enter
  748. end
  749. it "calls registered :leave actions" do
  750. leave = mock("leave")
  751. leave.should_receive(:leave).and_return { ScratchPad.record :leave }
  752. MSpec.register :leave, leave
  753. @state.process
  754. ScratchPad.recorded.should == :leave
  755. end
  756. end
  757. describe ContextState, "#it_should_behave_like" do
  758. before :each do
  759. @shared_desc = "shared context"
  760. @shared = ContextState.new(@shared_desc, :shared => true)
  761. MSpec.stub!(:retrieve_shared).and_return(@shared)
  762. @state = ContextState.new ""
  763. @a = lambda { }
  764. @b = lambda { }
  765. end
  766. it "raises an Exception if unable to find the shared ContextState" do
  767. MSpec.should_receive(:retrieve_shared).and_return(nil)
  768. lambda { @state.it_should_behave_like "this" }.should raise_error(Exception)
  769. end
  770. describe "for nested ContextState instances" do
  771. before :each do
  772. @nested = ContextState.new ""
  773. @nested.parents.unshift @shared
  774. @shared.children << @nested
  775. end
  776. it "adds nested describe blocks to the invoking ContextState" do
  777. @state.it_should_behave_like @shared_desc
  778. @shared.children.should_not be_empty
  779. @state.children.should include(*@shared.children)
  780. end
  781. it "changes the parent ContextState" do
  782. @shared.children.first.parents.first.should equal(@shared)
  783. @state.it_should_behave_like @shared_desc
  784. @shared.children.first.parents.first.should equal(@state)
  785. end
  786. end
  787. it "adds examples from the shared ContextState" do
  788. @shared.it "some", &@a
  789. @shared.it "thing", &@b
  790. @state.it_should_behave_like @shared_desc
  791. @state.examples.should include(*@shared.examples)
  792. end
  793. it "sets the containing ContextState for the examples" do
  794. @shared.it "some", &@a
  795. @shared.it "thing", &@b
  796. @shared.examples.each { |ex| ex.should_receive(:context=).with(@state) }
  797. @state.it_should_behave_like @shared_desc
  798. end
  799. it "adds before(:all) blocks from the shared ContextState" do
  800. @shared.before :all, &@a
  801. @shared.before :all, &@b
  802. @state.it_should_behave_like @shared_desc
  803. @state.before(:all).should include(*@shared.before(:all))
  804. end
  805. it "adds before(:each) blocks from the shared ContextState" do
  806. @shared.before :each, &@a
  807. @shared.before :each, &@b
  808. @state.it_should_behave_like @shared_desc
  809. @state.before(:each).should include(*@shared.before(:each))
  810. end
  811. it "adds after(:each) blocks from the shared ContextState" do
  812. @shared.after :each, &@a
  813. @shared.after :each, &@b
  814. @state.it_should_behave_like @shared_desc
  815. @state.after(:each).should include(*@shared.after(:each))
  816. end
  817. it "adds after(:all) blocks from the shared ContextState" do
  818. @shared.after :all, &@a
  819. @shared.after :all, &@b
  820. @state.it_should_behave_like @shared_desc
  821. @state.after(:all).should include(*@shared.after(:all))
  822. end
  823. end
  824. describe ContextState, "#filter_examples" do
  825. before :each do
  826. @state = ContextState.new ""
  827. @state.it("one") { }
  828. @state.it("two") { }
  829. end
  830. it "removes examples that are filtered" do
  831. @state.examples.first.stub!(:filtered?).and_return(true)
  832. @state.examples.size.should == 2
  833. @state.filter_examples
  834. @state.examples.size.should == 1
  835. end
  836. it "returns true if there are remaining examples to evaluate" do
  837. @state.examples.first.stub!(:filtered?).and_return(true)
  838. @state.filter_examples.should be_true
  839. end
  840. it "returns false if there are no remaining examples to evaluate" do
  841. @state.examples.first.stub!(:filtered?).and_return(true)
  842. @state.examples.last.stub!(:filtered?).and_return(true)
  843. @state.filter_examples.should be_false
  844. end
  845. end