0

我不明白为什么在它没有通过测试后第一次在控制台中输入“Video.all”时它返回空,但是如果我将它们指定为“video”和“video2”,然后我输入“Video.all”。全部”它正确返回 2 个视频。有谁知道为什么这不起作用?

应用程序/模型/video.rb

def self.search_by_title(search_term)
 if search_term
  find(:all, conditions: ['title LIKE ?', "%#{search_term}%"])
 else
  find(:all)
 end
end

我的模型测试:

规格/模型/video_spec.rb

  describe "#search_by_title" do
    let(:video) { Fabricate(:video, title: 'Family Guy', description: 'description1')}
    let(:video2) { Fabricate(:video, title: 'Futurama', description: 'description2')}

    it "should return all the videos if the keyword is found in video titles" do
      expect(Video.search_by_title("F")).to include(video2, video)
    end

    it "should return empty array when nothing is found" do
      expect(Video.search_by_title("Test Title")).to eq []
    end

    it "should return the video if the search keyword matches exactly" do
      expect(Video.search_by_title("Family Guy")).to include video
    end

    it "should return all the videos if the keyword is empty" do
      binding.pry
      expect(Video.search_by_title("")).to include(video2, video)
    end 

撬动的结果

@ line 33 :

    28:     it "should return the video if the search keyword matches exactly" do
    29:       expect(Video.search_by_title("Family Guy")).to include video
    30:     end
    31: 
    32:     it "should return all the videos if the keyword is empty" do
 => 33:       binding.pry
    34:       expect(Video.search_by_title("")).to include(video2, video)
    35:     end  
    36:   end
    37: 
    38:   describe "#average_ratings" do

[1] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1>)> Video.all
=> []
[2] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1>)> video
=> #<Video id: 1, title: "Family Guy", small_cover_url: nil, large_cover_url: nil, description: "description1", created_at: "2013-05-15 21:35:52", updated_at: "2013-05-15 21:35:52">
[3] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1>)> video2
=> #<Video id: 2, title: "Futurama", small_cover_url: nil, large_cover_url: nil, description: "description2", created_at: "2013-05-15 21:35:55", updated_at: "2013-05-15 21:35:55">
[4] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1>)> Video.all
=> [#<Video id: 1, title: "Family Guy", small_cover_url: nil, large_cover_url: nil, description: "description1", created_at: "2013-05-15 21:35:52", updated_at: "2013-05-15 21:35:52">,
 #<Video id: 2, title: "Futurama", small_cover_url: nil, large_cover_url: nil, description: "description2", created_at: "2013-05-15 21:35:55", updated_at: "2013-05-15 21:35:55">]
[5] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_1>)>
4

1 回答 1

0

即使您“解决”了它,您的代码也存在问题。

这就是search_by_title方法。

在您的控制台中尝试以下操作:

> def test(arg)
>   if arg
>     puts "there is arg"
>   else
>     puts "there is no arg"
>   end
> end
> a = ""
> test(a)
> #=> "there is arg"

看,你else永远不会被解雇,因为空字符串是 arg。

你需要像这样重构它

 if search_term.blank?
   find(:all)
 else
   find(:all, conditions: ['title LIKE ?', "%#{search_term}%"])
 end
于 2013-05-16T02:54:41.690 回答