【发布时间】:2018-09-29 11:12:19
【问题描述】:
我是 Ruby 和 RSpec 的新手,试图为字符串长度编写一个单位大小写。我有3个rb文件如下 1.调用文件
require_relative 'ruby_final_operations'
require_relative 'ruby_helper'
require 'uri'
require 'open-uri'
require 'prime'
module RubyOperations
# Public: Various commands for the user to interact with RubyCommand.
class Command
res = RubyOperations::Operations.new
res.letter_count(res.inputstr)
第二个文件 - 方法实现
require_relative 'ruby_helper'
require 'logger'
$FILE_LOG = RubyOperations.create_log(File.expand_path('~/RubyOperations_LOG.log'), Logger::DEBUG)
$STD_LOG = RubyOperations.create_log(nil, Logger::INFO)
module RubyOperations
class Operations
def inputstr
RubyOperations.log('Enter the String:[Length 20]',:BOTH)
@str = gets.chomp
raise StandardError if @str =~ /\d/ || @str.empty? || @str.length > 20
rescue StandardError,ArgumentError => e
RubyOperations.log(e,:ERROR)
end
def letter_count(str)
result = @str.length
RubyOperations.log("The number of letters in the string: #{result}",:BOTH)
end
第三个文件 - RSpec
require 'ruby_final_operations'
describe 'RubyOperations' do
describe 'Operations' do
subject = RubyOperations::Operations.new
describe '.letter_count' do
context 'when operation is provided' do
it 'returns letter count' do
allow(subject.letter_count("hello").to receive(:result).and_return(5)
end
end
end
问题在于,在第二个文件中,他的参数是“str”,但存储的键入字符串是“@str”。 如何从 rspec 文件中传递字符串 'hello' 来测试它。
【问题讨论】: