rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.18k stars 1.04k forks source link

Wrong variable name when generate scaffold with namespace & model-name options #2693

Closed taketo1113 closed 1 year ago

taketo1113 commented 1 year ago

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.2.2 Rails version: 7.0.8 RSpec version: 3.12

Observed behaviour

It wrong variable name with namepace prefix in the view specs, when is generated a namespace & model-name with scaffold.

$ rails g scaffold admin/user name:string --model-name=User
# spec/views/admin/posts/show.html.slim_spec.rb
RSpec.describe "admin/posts/show", type: :view do
  before(:each) do
    assign(:admin_post, User.create!())
  end
...
end
# spec/views/admin/posts/new.html.slim_spec.rb
RSpec.describe "admin/posts/new", type: :view do
  before(:each) do
    assign(:admin_post, User.new())
  end
...
end
# spec/views/admin/posts/edit.html.slim_spec.rb
RSpec.describe "admin/posts/edit", type: :view do
  let(:admin_post) {
    User.create!()
  }

  before(:each) do
    assign(:admin_post, admin_post)
  end

  it "renders the edit admin_post form" do
    render

    assert_select "form[action=?][method=?]", admin_post_path(admin_post), "post" do
    end
end

Expected behaviour

It is expected that the generated code is below.

# spec/views/admin/posts/show.html.slim_spec.rb
RSpec.describe "admin/posts/show", type: :view do
  before(:each) do
    assign(:post, User.create!())
  end
...
end
# spec/views/admin/posts/new.html.slim_spec.rb
RSpec.describe "admin/posts/new", type: :view do
  before(:each) do
    assign(:post, User.new())
  end
...
end
# spec/views/admin/posts/edit.html.slim_spec.rb
RSpec.describe "admin/posts/edit", type: :view do
  let(:post) {
    User.create!()
  }

  before(:each) do
    assign(:post, post)
  end

  it "renders the edit admin_post form" do
    render

    assert_select "form[action=?][method=?]", admin_post_path(post), "post" do
    end
end

Can you provide an example app?

https://github.com/taketo1113/rails-sample-rspec-rails/tree/view-spec-model-name

JonRowe commented 11 months ago

Fix released in 6.0.4 / 6.1.0