У меня есть код для чтения и непрочитанного статуса пользователя.Здесь, когда пользователь посещает сообщение / шоу, его идентификатор сохраняется в таблице posts_users
.Я пытаюсь написать rspec скрипт, который не запускается.Я использовал wait_for_ajax , но не смог.
Это мой posts/show.html.erb
<h4>Showing Post</h4>
<div class="row">
<div class="col-sm-4">
<div class="card" style="width: 20rem;">
<div class="card-body">
<h5 class="card-title"><%= @post.name %></h5>
<p class="card-text"><%= @post.description %></p>
<%= image_tag @post.image.url(:medium) %>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="card text-white bg-info mb-3">
<div class="card-body">
<h5 class="card-title"><h3>Related Tags</h3></h5>
<p class="card-text">
<% @post.tags.each do |tag| %>
<div>
<%= tag.name %> <br/>
</div>
<% end %>
</p>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="card border-success mb-3" style="max-width: 18rem;">
<div class="card-body text-success">
<p class="card-text">
<%= render "ratings/form" %>
</p>
</div>
</div>
</div>
</div>
<div class="card text-center">
<div class="card-header">
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Comments</a>
</li>
</ul>
</div>
<div class="card-body">
<h5 class="card-title"><%= render @post.comments.includes(:user) %></h5>
<p class="card-text"><%= render "comments/form" %></p>
</div>
</div>
<%= link_to 'Back', topic_posts_path(@topic) %>
<%= hidden_field_tag :post_id, params[:id] %>
<%= hidden_field_tag :topic_id, params[:topic_id] %>
<script>
$(document).ready(function () {
$.ajax({
url: '/topics/' + $('#topic_id').val() + '/posts/' + $('#post_id').val() + '/mark_as_read',
type: 'GET'
});
});
</script>
Это мой posts_controller
class PostsController < ApplicationController
before_action :set_topic
before_action :set_post, except: [:create, :new, :index]
def index
if params[:topic_id].to_i > 0
@posts = @topic.posts.all
else
@posts = Post.includes(:topic).references(:topic).all
@posts = @posts.order('topic desc')
end
@posts = @posts.eager_load(:ratings, :comments, :user, :users).paginate(page: params[:page], per_page: 10)
@tags = Tag.all
end
def show
@comment = @post.comments.new
end
def mark_as_read
@post.users << current_user unless @post.users.include?(current_user)
end
def new
@post = @topic.posts.new
@tags = Tag.all
end
def create
@post = @topic.posts.new(post_params)
@post.user_id = current_user.id
@posts = @topic.posts.paginate(page: params[:page], per_page: 10).eager_load(:user, :users, :comments, :ratings)
respond_to do |format|
if @post.save
format.html {redirect_to topic_posts_url}
format.js {render 'posts/create'}
else
p @post.inspect
format.js {render 'new'}
end
end
end
def edit
authorize! :edit, @post
@tags = Tag.all
end
def update
params[:post][:tag_ids] ||= []
if @post.update(post_params)
redirect_to topic_post_path(@topic, @post), notice: 'Post was successfully updated.'
else
render :edit
end
end
def destroy
authorize! :destroy, @post
if @post.destroy
redirect_to topic_posts_path(@topic), notice: 'Post was successfully destroyed.'
else
render 'posts/show'
end
end
private
def set_topic
if params[:topic_id].present?
@topic = Topic.find(params[:topic_id])
end
end
def set_post
@post = @topic.posts.find(params[:id])
end
def post_params
params.require(:post).permit(:name, :description, :image, :topic_id, {tag_ids: []}, :tags_attributes => [:name])
end
end
Это мой тест спецификации / возможностей
require 'spec_helper'
feature 'Status management',:js => true do
let!(:user) {FactoryBot.create(:user)}
scenario 'unread and read' do
@topic = FactoryBot.create(:topic)
@post = FactoryBot.create(:post, topic_id: @topic.id, user: user)
@rating = FactoryBot.create(:rating, post_id: @post.id)
@ratings = Rating.all
sign_in_with 'user1@example.com', 'Asdfgh1@'
visit topic_posts_path(@topic)
expect(page).to have_content('Unread')
visit topic_post_path(@topic, @post)
expect(page).to have_content('Showing Post')
wait_for_ajax
visit topic_posts_path(@topic)
@post.reload
expect(page).to have_content('Read')
end
def sign_in_with(email, password)
visit new_user_session_path
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Log in'
end
end
при запускеникогда не входит / не запускает скрипт.поэтому, когда я захожу на страницу индекса сообщений, отображается тот же непрочитанный статус.это моя ошибка
ожидается, что текст "Read" будет найден в "Store" user1@example.com Выйти Выводить список сообщений Назад Создать тему Описание Рейтинги Комментарии Статус Отличная тема Новая тема Отличный контент 1.0 нет Непрочитано Редактировать Удалить Февраль22, 2019, 9:09, user1@example.com ".(Однако он был найден 1 раз при поиске без учета регистра.)
0) Управление статусом не прочитано и прочитано? [31mFailure / Error: ожидайте (страница) .to have_content ('Чтение')? [0m?[31m ожидает найти текст «Читать» в «Магазине user1@example.com Выйти» Список сообщений Назад Сообщение Тема Описание Рейтинги Комментарии Статус Отличная тема Новая тема Отличный контент 1.0 нет Непрочитано Редактировать Удалить 22 февраля 2019 9:09 по user1 @ example.ком».(Однако он был найден 1 раз с использованием поиска без учета регистра.)? [0m? [36m # ./spec/features/post_controller_read_spec.rb:17:in `блок (2 уровня) в '? [0m
1 пример, 1 сбой, 0 пройдено
А это мой лог
Processing by PostsController#show as HTML
Parameters: {"topic_id"=>"1", "id"=>"1"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Topic Load (0.5ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Post Load (0.5ms) SELECT "posts".* FROM "posts" WHERE "posts"."topic_id" = ? AND "posts"."id" = ? LIMIT ? [["topic_id", 1], ["id", 1], ["LIMIT", 1]]
Rendering posts/show.html.erb within layouts/application
Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "posts_tags" ON "tags"."id" = "posts_tags"."tag_id" WHERE "posts_tags"."post_id" = ? [["post_id", 1]]
(0.4ms) SELECT AVG("ratings"."rating") FROM "ratings" WHERE "ratings"."post_id" = ? [["post_id", 1]]
(0.2ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."post_id" = ? AND "ratings"."rating" = ? [["post_id", 1], ["rating", 5]]
(0.3ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."post_id" = ? AND "ratings"."rating" = ? [["post_id", 1], ["rating", 4]]
(0.2ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."post_id" = ? AND "ratings"."rating" = ? [["post_id", 1], ["rating", 3]]
(0.2ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."post_id" = ? AND "ratings"."rating" = ? [["post_id", 1], ["rating", 2]]
(0.2ms) SELECT COUNT(*) FROM "ratings" WHERE "ratings"."post_id" = ? AND "ratings"."rating" = ? [["post_id", 1], ["rating", 1]]
Rendered ratings/_form.html.erb (8.8ms)
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 1]]
Rendered collection of templates [0 times] (0.0ms)
Rendered comments/_form.html.erb (11.6ms)
Rendered posts/show.html.erb within layouts/application (192.2ms)
Rendered C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/devise-4.5.0/app/views/devise/menu/_login_items.html.erb (0.1ms)
Rendered layouts/_appliationlayout.html.erb (0.5ms)
Completed 200 OK in 352ms (Views: 191.4ms | ActiveRecord: 5.8ms)
Started GET "/topics/1/posts" for 127.0.0.1 at 2019-02-22 14:39:57 +0530
Processing by PostsController#index as HTML
Parameters: {"topic_id"=>"1"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Topic Load (0.4ms) SELECT "topics".* FROM "topics" WHERE "topics"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Rendering posts/index.html.erb within layouts/application
SQL (0.6ms) SELECT DISTINCT "posts"."id" FROM "posts" LEFT OUTER JOIN "ratings" ON "ratings"."post_id" = "posts"."id" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "users" ON "users"."id" = "posts"."user_id" LEFT OUTER JOIN "posts_users" ON "posts_users"."post_id" = "posts"."id" LEFT OUTER JOIN "users" "users_posts" ON "users_posts"."id" = "posts_users"."user_id" WHERE "posts"."topic_id" = ? LIMIT ? OFFSET ? [["topic_id", 1], ["LIMIT", 10], ["OFFSET", 0]]
SQL (1.0ms) SELECT "posts"."id" AS t0_r0, "posts"."name" AS t0_r1, "posts"."description" AS t0_r2, "posts"."created_at" AS t0_r3, "posts"."updated_at" AS t0_r4, "posts"."topic_id" AS t0_r5, "posts"."image_file_name" AS t0_r6, "posts"."image_content_type" AS t0_r7, "posts"."image_file_size" AS t0_r8, "posts"."image_updated_at" AS t0_r9, "posts"."user_id" AS t0_r10, "ratings"."id" AS t1_r0, "ratings"."rating" AS t1_r1, "ratings"."post_id" AS t1_r2, "ratings"."created_at" AS t1_r3, "ratings"."updated_at" AS t1_r4, "comments"."id" AS t2_r0, "comments"."content" AS t2_r1, "comments"."created_at" AS t2_r2, "comments"."updated_at" AS t2_r3, "comments"."post_id" AS t2_r4, "comments"."name" AS t2_r5, "comments"."user_id" AS t2_r6, "users"."id" AS t3_r0, "users"."email" AS t3_r1, "users"."encrypted_password" AS t3_r2, "users"."reset_password_token" AS t3_r3, "users"."reset_password_sent_at" AS t3_r4, "users"."remember_created_at" AS t3_r5, "users"."created_at" AS t3_r6, "users"."updated_at" AS t3_r7, "users_posts"."id" AS t4_r0, "users_posts"."email" AS t4_r1, "users_posts"."encrypted_password" AS t4_r2, "users_posts"."reset_password_token" AS t4_r3, "users_posts"."reset_password_sent_at" AS t4_r4, "users_posts"."remember_created_at" AS t4_r5, "users_posts"."created_at" AS t4_r6, "users_posts"."updated_at" AS t4_r7 FROM "posts" LEFT OUTER JOIN "ratings" ON "ratings"."post_id" = "posts"."id" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "users" ON "users"."id" = "posts"."user_id" LEFT OUTER JOIN "posts_users" ON "posts_users"."post_id" = "posts"."id" LEFT OUTER JOIN "users" "users_posts" ON "users_posts"."id" = "posts_users"."user_id" WHERE "posts"."topic_id" = ? AND "posts"."id" = ? [["topic_id", 1], ["id", 1]]
(0.6ms) SELECT AVG("ratings"."rating") FROM "ratings" WHERE "ratings"."post_id" = ? [["post_id", 1]]
Rendered posts/_post_list.html.erb (16.2ms)
Rendered posts/index.html.erb within layouts/application (16.6ms)
Rendered C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/devise-4.5.0/app/views/devise/menu/_login_items.html.erb (0.3ms)
Rendered layouts/_appliationlayout.html.erb (0.9ms)
Completed 200 OK in 27ms (Views: 18.0ms | ActiveRecord: 3.5ms)