| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
mrubyc-test is an unit test framework for mruby/c, supporting basic assertions, stub and mock.
Add this line to your application's Gemfile:
gem 'mrubyc-test'And then execute:
$ bundle
Or install it yourself as:
$ gem install mrubyc-test
Assuming you are using mrubyc-utils to manage your project and rbenv to manage Ruby versions. It means you have Mrubycfile and .ruby-version in the top directory of your project.
Besides, you have to locate mruby model files that are the target of testing like mrblib/models/class_name.rb
And read here about why you should use mrubyc-utils.
This is an example of ESP32 project:
~/your_project $ tree . ├── Mrubycfile # Created by mrubyc-utils ├── .ruby-version # It should be mruby's version like 'mruby-3.0.0' ├── Makefile ├── build ├── components ├── main ├── mrblib │ └── models # Place your model class files here │ ├── class_name.rb # The testing target `ClassName` │ └── my_class.rb # The testing target `MyClass` │ └── loops │ ├── main.rb # Loop script isn't covered by mrubyc-test. use mrubyc-debugger │ └── sub.rb # Loop script isn't covered by mrubyc-test. use mrubyc-debugger └── sdkconfig
In the same directory:
$ mrubyc-utils test init
Then, some directories and files will be created in your project. Now you can run test because a sample test code was also created.
$ mrubyc-utils test
You should get some assertion failures. Take a look at test/sample_test.rb to handle the failures and find how to write your own test.
def assertions
my_var = 1
assert_equal 1, my_var # => success
assert_not_equal 2, my_var # => success
assert_not_nil my_var # => success
endAssuming you have a model file at mrblib/models/sample.rb
class Sample
attr_accessor :result
def do_something(arg)
@result = arg + still_not_defined_method
end
endThen you can test #do_something method without having #still_not_defind_method like this:
def stub_case
sample_obj = Sample.new
stub(sample_obj).still_not_defined_method { ", so we are nice" }
sample_obj.do_something("Ruby is nice")
assert_equal 'Ruby is nice, so we are nice', sample_obj.result
endmrblib/models/sample.rb looks like this time:
class Sample
def do_other_thing
is_to_be_hit(1, 2) # Two args for example
end
endYou can test whether #is_to_be_hit(v1, v2) method will be called:
def mock_case
sample_obj = Sample.new
mock(sample_obj).is_to_be_hit(2) # `2` should be count of parameters
sample_obj.do_other_thing
endThe API design and implementation of this gem is greatly inspired by test-unit. Thank the great work.
Bug reports and pull requests are welcome on GitHub at https://github.com/mrubyc/mrubyc-test. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the The 3-Clause BSD License.
Everyone interacting in the Mrubyc::Test project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
| Back | FazBrowse Home | New Git URL |