2010-03-28 7 views
0

테스트에서 Machinist 및 Shoulda를 제대로 사용하는 데 문제가 있습니다. 여기 Shinda 테스트에서 Machinist로 로그인 할 때 오류가 발생했습니다.

내 테스트입니다 :

context "on POST method rating" do 
p = Product.make 
u = nil 
setup do 
    u = login_as 
    post :vote, :rating => 3, :id => p 
end 

should "set rating for product to 3" do 
    assert_equal p.get_user_vote(u), 3 
end 

그리고 여기 내 청사진 :

Sham.login { Faker::Internet.user_name } 
Sham.name { Faker::Lorem.words} 
Sham.email { Faker::Internet.email} 
Sham.body { Faker::Lorem.paragraphs(2)} 

User.blueprint do 
login 
password "testpass" 
password_confirmation { password } 
email 
end 

Product.blueprint do 
name {Sham.name} 
user {User.make} 
end 

그리고 내 인증 시험 도우미 :

def login_as(u = nil) 
    u ||= User.make() 
    @controller.stubs(:current_user).returns(u) 
    u 
end 

내가 오류는 다음과 같습니다

/home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/validations.rb:1090:in `save_without_dirty!': Validation failed: Login has already been taken, Email has already been taken (ActiveRecord::RecordInvalid)               
     from /home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/dirty.rb:87:in `save_without_transactions!'                
     from /home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/transactions.rb:200:in `save!'                   
     from /home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'         
     from /home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/transactions.rb:182:in `transaction'                  
     from /home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/transactions.rb:200:in `save!'                   
     from /home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/transactions.rb:208:in `rollback_active_record_state!' 
     from /home/jason/moderndarwin/vendor/rails/activerecord/lib/active_record/transactions.rb:200:in `save!' 
     from /usr/lib/ruby/gems/1.8/gems/machinist-1.0.6/lib/machinist/active_record.rb:55:in `make' 
     from /home/jason/moderndarwin/test/blueprints.rb:37 
     from /usr/lib/ruby/gems/1.8/gems/machinist-1.0.6/lib/machinist.rb:77:in `generate_attribute_value' 
     from /usr/lib/ruby/gems/1.8/gems/machinist-1.0.6/lib/machinist.rb:46:in `method_missing' 
     from /home/jason/moderndarwin/test/blueprints.rb:37 
     from /usr/lib/ruby/gems/1.8/gems/machinist-1.0.6/lib/machinist.rb:20:in `instance_eval' 
     from /usr/lib/ruby/gems/1.8/gems/machinist-1.0.6/lib/machinist.rb:20:in `run' 
     from /usr/lib/ruby/gems/1.8/gems/machinist-1.0.6/lib/machinist/active_record.rb:53:in `make' 
     from ./test/functional/products_controller_test.rb:25:in `__bind_1269805681_945912' 
     from /home/jason/moderndarwin/vendor/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:293:in `call' 
     from /home/jason/moderndarwin/vendor/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:293:in `merge_block' 
     from /home/jason/moderndarwin/vendor/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:288:in `initialize' 
     from /home/jason/moderndarwin/vendor/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:169:in `new' 
     from /home/jason/moderndarwin/vendor/gems/thoughtbot-shoulda-2.10.2/lib/shoulda/context.rb:169:in `context' 
     from ./test/functional/products_controller_test.rb:24 

내가 뭘 잘못하고 있는지 알아낼 수 없다 ... 내 user_controller 테스트에서 내 auth (Authlogic)로 login_as를 테스트했다.

올바른 방향으로 어떤 포인터를 주시면 감사하겠습니다!

나는 부분적인 해결책을 가지고

답변

0

... 나는 위의 문제를 해결했습니다,하지만 should_redirect_to 기능은 여전히 ​​... 나에게 맞는주고있다

이 나의 새로운 테스트 코드입니다 :

context "on POST method rating" do 

    setup do 
    @u = login_as 
    @p1 = Product.make 
    @p2 = Product.make # Make a second product that we'll show after rating the first 
    post :vote, :rating => 3, :id => @p1 
    end 

    should "set rating for product to 3" do 
    assert_equal @p1.get_user_vote(@u), 3 
    end 

    should_redirect_to("product page") {show_product_url(:id => @p2)}  
end 

그리고 나의 새로운 오류 :

response to be a redirect to <http://test.host/products/555660218> but was a redirect to <http://test.host/products>. 

어떤 아이디어가 여기에서 갈?

0

컨트롤러가 products_url로 리디렉션됩니까?

사이드 노트 : 고유성이 필요한 유효성 검사에 임의의 데이터 (Faker)를 사용하면 안됩니다. 그게 바로 공장 소녀의 순서입니다. 나는 기계공이 비슷한 것을 가지고 있다고 가정한다. 행동 => '쇼', : ID => current_user.next_product (세션 [: 태그]) 데프 Vote.do_vote 번호 일부 투표 로직 redirect_to 할 투표 : 여기

+0

내 컨트롤러가하고있는 것입니다 끝을 – user303747

+0

필자가 이해하지 못하는 것은 테스트 스위트를 사용할 때 브라우저에서 테스트 할 때 코드가 제대로 작동한다는 것입니다. POST에 상황을 "나는 약간의 미지수를 제거하기 위해 조롱 시도, 또한 끝 – user303747

+0

: 데프 redirect_to의 show_product_url 투표() current_user.next_product (세션 [: 태그]) 나는 또한 내 테스트에 맞게 컨트롤러 리디렉션을 다시 작성하려고했습니다 방법 평가 "할 설정 할 @u = login_as @ P1 = Product.make @ P2 = Product.make # 우리가 처음 게시물을 평가 한 후 보여주지 두 번째 제품 확인 : 투표 : 등급 => 3, : id => @ p1 @ u.expects (: next_product) .returns (@ p2) end should_redirect_to ("제품 페이지") {show_product_url (: id => @ p2)} end 여전히 같은 오류입니다. – user303747

관련 문제