class Tset::Writers::ModelTest
Writes the test for a model for each testable codes given
@param model_name
[String] a name of the model (e.g. post) @param testables [Array] an array of lines of code returned by Tset::Analyzers::Model
@param framework [String] a test framework. Default to 'rspec'.
Attributes
framework[R]
model_name[R]
target[R]
testables[R]
Public Class Methods
new(model_name, testables, framework = 'rspec')
click to toggle source
# File lib/tset/writers/model_test.rb, line 17 def initialize(model_name, testables, framework = 'rspec') @model_name = model_name @testables = testables @framework = framework @target = Pathname.pwd.realpath end
Public Instance Methods
class_name()
click to toggle source
@return [String] A classified version of the name (e.g. Post)
# File lib/tset/writers/model_test.rb, line 28 def class_name @class_name ||= model_name.classify end
start!()
click to toggle source
# File lib/tset/writers/model_test.rb, line 32 def start! case framework when 'rspec' testables.each { |testable| write_rspec(testable) } when 'minitest' end end
Private Instance Methods
_rspec_file()
click to toggle source
# File lib/tset/writers/model_test.rb, line 84 def _rspec_file target.join("spec/models/#{ @model_name }_spec.rb") end
describe(category)
click to toggle source
Writes a describe block
@param category [String] notion that is being described
# File lib/tset/writers/model_test.rb, line 61 def describe(category) file_contents = File.read(_rspec_file) replacement = %Q(describe #{class_name} do describe "#{category}" do end) file_contents.gsub!(/describe #{class_name} do/, replacement) File.write(_rspec_file, file_contents) end
expectation(test)
click to toggle source
Writes an expectation
@param test [Tset::Tset]
# File lib/tset/writers/model_test.rb, line 76 def expectation(test) file_contents = File.read(_rspec_file) replacement = %Q( describe "#{test.category}" do #{test.code}) file_contents.gsub!(/\s\sdescribe "#{test.category}" do/, replacement) File.write(_rspec_file, file_contents) end
write_rspec(testable)
click to toggle source
Translates the testable into rspec and writes to rspec file
@param testable [Tset::Testable]
# File lib/tset/writers/model_test.rb, line 47 def write_rspec(testable) test = testable.to_test('rspec') file_contents = File.read(_rspec_file) describe(test.category) unless file_contents =~ /describe "#{test.category}" do/ expectation(test) end