首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Shoulda测试中使用Machinist登录时出错

在Shoulda测试中使用Machinist登录时出错
EN

Stack Overflow用户
提问于 2010-03-29 04:03:22
回答 2查看 514关注 0票数 0

在我的测试中,我在正确使用Machinist和Shoulda时遇到了一些麻烦。

这是我的测试:

代码语言:javascript
复制
 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

这是我的蓝图:

代码语言:javascript
复制
 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

和我的身份验证测试助手:

代码语言:javascript
复制
def login_as(u = nil)
   u ||= User.make()
   @controller.stubs(:current_user).returns(u)
   u
 end

我得到的错误是:

代码语言:javascript
复制
/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测试中使用我的身份验证(Authlogic)测试了login_as。

任何指向正确方向的指针都将不胜感激!

EN

回答 2

Stack Overflow用户

发布于 2010-03-29 05:34:44

我有一个部分解决方案..。我已经解决了上面的问题,但是should_redirect_to函数仍然让我不知所措。

这是我的新测试代码:

代码语言:javascript
复制
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

我的新错误是:

代码语言:javascript
复制
response to be a redirect to <http://test.host/products/555660218> but was a redirect to <http://test.host/products>.

你有什么想法吗?

票数 0
EN

Stack Overflow用户

发布于 2010-03-29 10:06:31

您的控制器是否重定向到products_url?

附注:对于任何需要唯一性的验证,都不应该使用随机数据(Faker)。这就是“工厂女孩”系列的用处。我猜Machinist也有类似的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2534331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档