我在測試中正確使用機械師和Shoulda時遇到了一些麻煩。在Shoulda測試中使用機械師登錄時出錯
這裏是我的測試:
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。
任何指針正確的方向將不勝感激!
這裏是我的控制器是這樣做的: 高清投票 Vote.do_vote#做一些表決邏輯 redirect_to的:行動=> '秀',:ID => current_user.next_product(會話[:標籤]) 結束 – user303747 2010-04-08 06:03:09
我不明白的是,當我在瀏覽器中測試時,代碼正常工作,而不是在使用測試套件時。我也試着重寫器重定向到我的測試賽: 高清投票 redirect_to的show_product_url(current_user.next_product(會話[:標籤])) 結束 – user303747 2010-04-08 06:35:58
此外,我試圖嘲弄消除一些未知:在POST 語境」方法的評價」做 設置做 @u = login_as @ P1 = Product.make @ P2 = Product.make#做第二個產品,我們將評級第一 後後顯示:投票:等級=> 3::id => @ p1 @ u.expects(:next_product).returns(@ p2) end should_redirect_to(「product page」){show_product_url(:id => @ p2)} 結束 仍然是一樣的錯誤。 – user303747 2010-04-08 06:36:34