簡體   English   中英

RSpec和奇怪的測試結果

[英]RSpec and weird tests results

我正在嘗試制作一個簡單的應用程序。 當我在瀏覽器中測試它時,一切都可以正常工作。 但是,當我嘗試使用RSpec(2.5)運行一些測試時,在:create controller的測試中失敗了。

這是我的創建方法:

def create
 @website = Website.new(params[:website])
 if @website.save

   flash[:notice] = "Website created."
   redirect_to(:action => 'list')
 else
   render('new')
 end
end

控制器測試:

describe WebsitesController do
  render_views
  .
  .
  .
  describe "POST 'create'" do
    before(:each) do
      @attr = { :adres => "www.excc.pl", :opis => "aaa "*22, :tagi => "aaa aaa aaa",
                :preview => File.new(Rails.root + 'spec/fixtures/rails.png'),
                :preview_mini => File.new(Rails.root + 'spec/fixtures/rails.png')}
    end
    describe "success" do
      it "should have the right title" do
        response.should have_selector("title", :content=>"Lista witryn w portfolio")
      end
    end
  .
  .
  .

測試結果:

  1) WebsitesController POST 'create' should have the right title
     Failure/Error: response.should have_selector("title", :content=>"Lista witryn    w portfolio")
     expected following output to contain a <title>Lista witryn w portfolio</title> tag:
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
     # ./spec/controllers/websites_controller_spec.rb:34:in `block (4 levels) in

website_controller_spec.rb:34是指創建方法

但是,此測試已正確通過(對於不正確的數據,應將其重定向回具有指定標題的“新”站點):

it "should have the right title" do
    post :create, :website => @attr.merge(:adres => "")
    response.should have_selector("title", :content=>"Dodaj stronę WWW")
end

第二個問題是...有一段時間,我得到了這樣的測試結果:

<html><body>You are being <a href="http://test.host/websites/list">redire cted</a>.</body></html>

...這使我拉了一段時間,直到我做完某事(我真的不知道是什么)然后它就消失了。 但是,當我認為它將來會回來毀滅我的幸福時,這讓我感到恐懼。

任何對此的想法將不勝感激。

很難知道這里要問的是什么,但是我相信問題是您沒有為成功/失敗設定條件。 如果我理解正確,當您傳遞空白的:adres屬性時,保存將失敗並且頁面將呈現list動作。 因此,您想存根create方法並根據預期結果返回true或false:

it "succeeds" do
  @website = mock_model(Website,:save=>true)
  Website.stub(:new) { @website }
  post :create, :website => {}
  # redirects
  response.should have_selector("etc etc")
end


it "fails" do
  @website = mock_model(Website,:save=>false)
  Website.stub(:new) { @website }
  post :create, :website => {}
  # renders 'new'
  response.should_not have_selector("etc etc")
end

參數有效性的測試應在模型規格中執行:

@website = Website.new(:adres=>"")
@website.should_not be_valid

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM