1

私の実際のプロジェクトでは、テストに次の宝石を使用しています。

  • リビジョン 6641fddcfc337a3ddaa84ac59272e884090332c3 の git://github.com/jnicklas/capybara.git のカピバラ
  • rails (3.1.0.rc5) (およびその要件)
  • factory_girl (2.0.1)
  • factory_girl_rails (1.1.0)
  • rspec (2.6.0)
  • rspec-コア (2.6.4)
  • rspec レール (2.6.1)

実行するrake specと、次のエラーが表示されます。

...F.....*................*....*.*.*.

Pending:
  <pending snipped out>

Failures:

  1) Articles GET /articles/:id should show the article when clicking it
     Failure/Error: page.should have_content a.body
       expected there to be content "Dieser Artikel ist nur zum testen, erfüllt keinen Sinn und langweilig ist mir ohnehin. Das sollte nur mal so gesagt werden... MfG Euer Admin!\n" in "Stars3\n  \n    Stars!Artikel - 1 - This is an article created just for testing purpose\n  \n  \n    Artikel\n\n    \nDieser Artikel ist nur zum testen, erfüllt keinen Sinn und langweilig ist mir ohnehin. Das sollte nur mal so gesagt werden... MfG Euer Admin!\n\n  \n"              
     # ./spec/requests/articles_spec.rb:46

Finished in 1.96 seconds
37 examples, 1 failure, 5 pending

Failed examples:

rspec ./spec/requests/articles_spec.rb:40 # Articles GET /articles/:id should show the article when clicking it

期待と結果の間に違いが見られません...これを機能させるために正しい方向に向けてもらえますか?

仕様:

describe "GET /articles/:id" do
  it "should show the article when clicking it", :type => :request do      
    a = Factory.create(:article)#, :user => u)
    a.save
    visit articles_path
    click_link a.title
    page.should have_content a.title
    page.should have_content a.body
  end
end

工場:

Factory.define :article do |a|
  Factory.sequence :title do |i|
    "#{1} - This is an article created just for testing purpose"
  end

  a.title { Factory.next(:title) }
  a.association :user, :factory => :user #user  { User.first }
  a.body <<eot
Dieser Artikel ist nur zum testen, erfüllt keinen Sinn und langweilig ist mir ohnehin. Das sollte nur mal so gesagt werden... MfG Euer Admin!
eot

end

景色:

<% title "Artikel - #{@article.title}" -%>

<%= @article.compiled -%>

コントローラー:

class ArticlesController < ApplicationController
  before_filter :login_required, :except => [:index, :show]

  def show
    @article = Article.find_by_title(params[:id])
  end

end

そしてモデル:

class Article < ActiveRecord::Base
  validates_presence_of :body, :message => "Es muss schon Text drin stehen"
  validates_presence_of :title, :message => "Du brauchst nen Titel für den Artikel"
  validates_presence_of :user, :message => "Das hätte nicht passieren dürfen... Es wurde kein Nutzer angegeben"
  belongs_to :user
#  before_save :compile_body

  def compiled
    body
  end

  def to_param
    "#{title}"
  end

end

足りないものがあれば、ここに載せますので、お気軽にお尋ねください。

4

1 に答える 1

1

ファクトリを次のように変更して、記事の本文を単純なワンライナーに設定します。

Factory.define :article do |a|
  Factory.sequence :title do |i|
    "#{1} - This is an article created just for testing purpose"
  end

  a.title { Factory.next(:title) }
  a.association :user, :factory => :user
  a.body { "Dieser Artikel ist ein Test" }

end

テストは合格しましたか。

于 2011-07-30T13:40:17.873 に答える