Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 可以在rspec特性测试中调用truncate方法吗?_Ruby On Rails 3_Capybara_Rspec2_Truncate - Fatal编程技术网

Ruby on rails 3 可以在rspec特性测试中调用truncate方法吗?

Ruby on rails 3 可以在rspec特性测试中调用truncate方法吗?,ruby-on-rails-3,capybara,rspec2,truncate,Ruby On Rails 3,Capybara,Rspec2,Truncate,我使用的是Rails3.2、RSpec2和Capybara2 我正在截断我的网页中的正文字符串,并希望在我的功能规范中对此进行测试 有人能告诉我怎么做吗,因为我在测试中似乎不能调用truncate,因为它一直说这是一个未定义的方法,我真的不想在测试套件中有一长串被截断的文本;仅仅应用截断函数就足够了 我尝试过使用helper.truncate(),view.truncate(),但没有效果 我已经在测试的其他部分使用了Faker gem,如果有办法生成lorem ipsum字符串并将其截断以进行

我使用的是Rails3.2、RSpec2和Capybara2

我正在截断我的网页中的正文字符串,并希望在我的功能规范中对此进行测试

有人能告诉我怎么做吗,因为我在测试中似乎不能调用truncate,因为它一直说这是一个未定义的方法,我真的不想在测试套件中有一长串被截断的文本;仅仅应用截断函数就足够了

我尝试过使用
helper.truncate()
view.truncate()
,但没有效果

我已经在测试的其他部分使用了Faker gem,如果有办法生成lorem ipsum字符串并将其截断以进行比较的话

查看代码:

<dd><%= truncate(project.details, :length => 100) %></dd>`
当我显示完整的详细信息文本时,这个测试运行得很好,但是因为我决定在这个视图中只显示前100个字符,所以我不确定如何测试它,而不必将详细信息设置为固定字符串,然后以某种方式检查它的截断版本

谢谢


Col

truncate
字符串
类的Rails扩展,因此在Ruby或RSpec中不可用。也许有办法以某种方式“包括”它,但我很熟悉如何做到这一点。然而,事实证明,这个定义是自包含的,所以自己定义它是很简单的。这是来源于。它被定义为一个实例方法,因此如果您想将它与传入的字符串一起使用,您需要包括一个
text
参数,以代替当前在正文第一行中所做的引用
self

# File activesupport/lib/active_support/core_ext/string/filters.rb, line 38
  def truncate(length, options = {})
    text = self.dup
    options[:omission] ||= "..."

    length_with_room_for_omission = length - options[:omission].mb_chars.length
    chars = text.mb_chars
    stop = options[:separator] ?
      (chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission

    (chars.length > length ? chars[0...stop] + options[:omission] : text).to_s
  end

truncate
String
类的Rails扩展,因此在Ruby或RSpec中不可用。也许有办法以某种方式“包括”它,但我很熟悉如何做到这一点。然而,事实证明,这个定义是自包含的,所以自己定义它是很简单的。这是来源于。它被定义为一个实例方法,因此如果您想将它与传入的字符串一起使用,您需要包括一个
text
参数,以代替当前在正文第一行中所做的引用
self

# File activesupport/lib/active_support/core_ext/string/filters.rb, line 38
  def truncate(length, options = {})
    text = self.dup
    options[:omission] ||= "..."

    length_with_room_for_omission = length - options[:omission].mb_chars.length
    chars = text.mb_chars
    stop = options[:separator] ?
      (chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission

    (chars.length > length ? chars[0...stop] + options[:omission] : text).to_s
  end
这对我有用:)

这对我有用:)


最后,我决定如果在更大的屏幕上查看页面,那么更宽的列将允许显示更多的细节,并减少截断文本的需要,因此我决定使用css来修剪细节,而不是使用以下内容来减少屏幕上显示的内容:

.text-trim {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

最后,我决定如果在更大的屏幕上查看页面,那么更宽的列将允许显示更多的细节,并减少截断文本的需要,因此我决定使用css来修剪细节,而不是使用以下内容来减少屏幕上显示的内容:

.text-trim {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

请分享您的规范,以便我们对其进行评论。嗨,Peter Alfvin,我在问题中添加了查看和测试代码。请分享您的规范,以便我们对其进行评论。嗨,Peter Alfvin,我在问题中添加了查看和测试代码。