2010-03-28 30 views
0

J'ai de la difficulté à utiliser correctement Machinist et Shoulda dans mes tests.Erreur lors de la connexion avec Machinist dans le test de Shoulda

Voici mon test:

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 

Et voici mes plans:

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 

Et mon aide de test d'authentification:

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

L'erreur que je reçois est:

/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 

Je n'arrive pas à comprendre ce que je fais de mal ... J'ai testé le login_as avec mon auth (Authlogic) dans mon test user_controller.

Tout pointeur dans la bonne direction serait très apprécié!

Répondre

0

J'ai une solution partielle ... Je l'ai résolu le problème ci-dessus, mais la fonction est should_redirect_to encore me envoie FITS ...

Ceci est mon nouveau code de test:

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 

Et ma nouvelle erreur:

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

Toutes les idées où aller d'ici?

0

Votre contrôleur redirige-t-il vers products_url? Remarque: vous ne devez pas utiliser de données aléatoires (Faker) pour toute validation nécessitant un caractère unique. C'est à ça que servent les séquences de Factory Girl. Je suppose que Machinist a quelque chose de similaire.

+0

Voici ce que mon contrôleur fait: vote def Vote.do_vote # font une logique de vote redirect_to: action => 'show',: id => current_user.next_product (session [: tags]) fin – user303747

+0

Ce que je ne comprends pas, c'est que le code fonctionne correctement lorsque je le teste dans un navigateur, mais pas quand j'utilise la suite de tests. J'ai aussi essayé de réécrire la redirection du contrôleur pour correspondre à mon test: voter def redirect_to show_product_url (current_user.next_product (session [: tags])) fin – user303747

+0

De plus, j'ai essayé de se moquant d'éliminer certaines inconnues: contexte « sur POST méthode note » do setup faire @u = login_as @ p1 = Product.make @ p2 = Product.make # faire un deuxième produit que nous allons montrer, après avis du premier après: vote,: note => 3,: id => @ p1 @ u.expects (: next_product) .Retour (@ p2) fin should_redirect_to ("fiche produit") {show_product_url (: id => @ p2)} fin Toujours la même erreur. – user303747