Rspec act as null object
See also the Wikipedia entry on the Null Object Pattern. The RSpec Book explains this. I recommend you to read it. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Ask Question. Asked 9 years, 8 months ago. Active 9 years, 3 months ago. Make sure to use the same version for cucumber-junit that you are using for cucumber-java or cucumber-java8. TestNG assertions are similar JUnit. For a more extensive example of how to use TestNG with Cucumber, see the java-calculator-testng example.
We recommend using Node. You can use any other assertion library if you wish. Here is an example using Chai :. Add the rspec-expectations gem to your Gemfile. When, in RSpec, something. Use the double method, passing in an optional identifier, to create one:. Most of the time you will want some confidence that your doubles resemble an existing object in your system. Verifying doubles are provided for this purpose. If the existing object is available, they will prevent you from adding stubs and expectations for methods that do not exist or that have an invalid number of parameters.
Verifying doubles have some clever tricks to enable you to both test in isolation without your dependencies loaded while still being able to validate them against real objects.
More detail is available in their documentation. A method stub is an implementation that returns a pre-determined value. Method stubs can be declared on test doubles or real objects using the same syntax. You can also use this shortcut, which creates a test double and declares a method stub in one statement:. The first argument is a name, which is used for documentation and appears in failure messages.
This is particularly nice when providing a list of test doubles to a method that iterates through them:. The invocations cycle through the list. The last value is returned for any subsequent invocations:. A message expectation is an expectation that the test double will receive a message some time before the example ends. If the message is received, the expectation is satisfied. If not, the example fails. Verifies the given object received the expected message during the course of the test.
For a message to be verified, the given object must be setup to spy on it, either by having it explicitly stubbed or by being a null object double e. Convenience methods are provided to easily create null object doubles for this purpose:. For example, this does not work properly:. A method stub is an instruction to an object real or test double to return a known value in response to a message:. This tells the die object to return the value 3 when it receives the roll message.
A message expectation is an expectation that an object should receive a specific message during the course of a code example:. The documentation for rspec-mocks is a work in progress. We'll be adding Cucumber features over time, and clarifying existing ones. If you have specific features you'd like to see added, find the existing documentation incomplete or confusing, or, better yet, wish to write a missing Cucumber feature yourself, please submit an issue or a pull request.
0コメント