Guest User

Untitled

a guest
Nov 21st, 2017
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. # Users
  2. User.create!(name: "Example User",
  3. email: "example@railstutorial.org",
  4. password: "foobar",
  5. password_confirmation: "foobar",
  6. admin: true,
  7. activated: true,
  8. activated_at: Time.zone.now)
  9. 99.times do |n|
  10. name = Faker::Name.name
  11. email = "example-#{n+1}@railstutorial.org"
  12. password = "password"
  13. User.create!(name: name,
  14. email: email,
  15. password: password,
  16. password_confirmation: password,
  17. activated: true,
  18. activated_at: Time.zone.now)
  19. end
  20.  
  21. # Microposts
  22. users = User.order(:created_at).take(6)
  23. image_path = File.join(Rails.root, "test/fixtures/images/sample.jpg")
  24. 50.times do
  25. content = Faker::Lorem.sentence(5)
  26. users.each { |user|
  27. # 画像と一緒にマイクロポストの初期データを作成する
  28. user.microposts.create!(content: content, picture: File.new(image_path))
  29. }
  30. end
  31.  
  32. # Following relationships
  33. users = User.all
  34. user = users.first
  35. following = users[2..50]
  36. followers = users[3..40]
  37. following.each { |followed| user.follow(followed) }
  38. followers.each { |follower| follower.follow(user) }
  39.  
  40. schema.rb
  41. create_table "hoges"
  42. t.binary "photo", limit: 65535
  43. end
  44.  
  45. path = Rails.root.join("画像が置いてあるパス")
  46. photo = File.open(path, "rb").read
  47.  
  48. require 'active_record/fixtures'
  49. module FileFixtureExtension
  50. def file(file_name)
  51. File::open(Rails.root.join('test/fixtures/', file_name), 'rb') do |f|
  52. "!!binary "#{Base64.strict_encode64(f.read)}""
  53. end
  54. end
  55. end
  56. ActiveRecord::Fixture.extend FileFixtureExtension
  57.  
  58. one:
  59. user: :one
  60. file: <%= ActiveRecord::Fixture::file 'files_to_upload/test.png' %>
  61.  
  62. sudo apt search ag|grep "silver"
  63. silversearcher-ag-el/xenial,xenial 0.46-2 all
  64. sudo apt install silversearcher-ag
  65.  
  66. bundle exec rails test
  67. .../config/initializers/file_fixtures_extension.rb:11:in `<top
  68. (required)>': uninitialized constant Fixture (NameError)
  69. from /home/d/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:655:in `block in load_config_initializer'
  70.  
  71. cd /home/d/.rvm/gems/ruby-2.4.0/gems
  72. ag 'class Fixture '
  73. activerecord-5.1.4/lib/active_record/fixtures.rb
  74. 11: class FixtureClassNotFound < ActiveRecord::ActiveRecordError #:nodoc:
  75. 412: class FixtureSet
  76. 804: class Fixture #:nodoc:
  77. 807: class FixtureError < StandardError #:nodoc:
  78.  
  79. test "should update user" do
  80. #file = Rack::Test::UploadedFile.new(Rails.root.join("test/fixtures/files_to_upload/test.png"))
  81. file = fixture_file_upload('/files_to_upload/test.png','image/png')
  82. patch user_url(@user),
  83. params: {
  84. user: {
  85. name:'new name',
  86. file: file,
  87. }
  88. }
  89. assert_redirected_to user_url(@user)
  90. end
Add Comment
Please, Sign In to add comment