最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Incongruence between rails guide and test error message - Stack Overflow

programmeradmin3浏览0评论

The following unit test, purposely blank

class CustomMailerTest < ActionMailer::TestCase
  test "message_email" do
  end
end

where the class CustomMailer defines a message_email method.

The rails guide for testing, in particular mailers, states for a mailer named UserMailer, the fixtures should reside in test/fixtures/user_mailer directory.

Directory fixtures/custom_mailer has a message_email.yml file.

If the YAML file is structured as fixtures normally are default-generated:

one:
  name: MyString

Running the test returns

CustomMailerTest#test_message_email:
ActiveRecord::StatementInvalid: Could not find table 'custom_mailer_message_email'

Removing the parent one: and resetting the YAML to be valid (removing excess spacing) results in

ActiveRecord::Fixture::FormatError: fixture key is not a hash:  
/Users/[..]/test/fixtures//custom_mailer/message_email.yml,  
keys: ["name", "email", "phone", "message_body"]

The subsequent example of a fixture, with various attempts,

Hi [email protected],

You have been invited.

Cheers!

returns a YAML formatting error

ActiveRecord::Fixture::FormatError: YAML syntax error occurred while parsing /Users[...]/test/fixtures//custom_mailer/message_email.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): mapping values are not allowed in this context at line 10 column 12

or a

ActiveRecord::Fixture::FormatError: fixture is not a hash: /Users[...]/test/fixtures//custom_mailer/message_email.yml

So what format should the fixture file have & what should it file look like, given the quoted keys?

The following unit test, purposely blank

class CustomMailerTest < ActionMailer::TestCase
  test "message_email" do
  end
end

where the class CustomMailer defines a message_email method.

The rails guide for testing, in particular mailers, states for a mailer named UserMailer, the fixtures should reside in test/fixtures/user_mailer directory.

Directory fixtures/custom_mailer has a message_email.yml file.

If the YAML file is structured as fixtures normally are default-generated:

one:
  name: MyString

Running the test returns

CustomMailerTest#test_message_email:
ActiveRecord::StatementInvalid: Could not find table 'custom_mailer_message_email'

Removing the parent one: and resetting the YAML to be valid (removing excess spacing) results in

ActiveRecord::Fixture::FormatError: fixture key is not a hash:  
/Users/[..]/test/fixtures//custom_mailer/message_email.yml,  
keys: ["name", "email", "phone", "message_body"]

The subsequent example of a fixture, with various attempts,

Hi [email protected],

You have been invited.

Cheers!

returns a YAML formatting error

ActiveRecord::Fixture::FormatError: YAML syntax error occurred while parsing /Users[...]/test/fixtures//custom_mailer/message_email.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): mapping values are not allowed in this context at line 10 column 12

or a

ActiveRecord::Fixture::FormatError: fixture is not a hash: /Users[...]/test/fixtures//custom_mailer/message_email.yml

So what format should the fixture file have & what should it file look like, given the quoted keys?

Share edited Feb 16 at 10:57 Jerome asked Feb 16 at 10:45 JeromeJerome 6,2173 gold badges37 silver badges91 bronze badges 4
  • 1 don't name it .yml, these are just plain text. – Alex Commented Feb 16 at 11:18
  • it turns out the file should be without extension, as even .txtadded will lead to same error(s). – Jerome Commented Feb 17 at 4:18
  • .txt works for me – Alex Commented Feb 17 at 12:58
  • mine was on MacOS – Jerome Commented Feb 17 at 15:32
Add a comment  | 

1 Answer 1

Reset to default 2

As noted on the Rails guides, mailer fixtures are different from the standard ones in the sense those are not ActiveRecord data. That's the reason your first example failed (Could not find table error).

The creation of the fixture as a .yml file led Rails to search for an active record matching the same specifications of your fixture. You don't have a db table / model definition in those conditions, which led to that error.

That's also the reason why updating the .yml structure on your second attempt didn't help solve the problem. To build a email fixture, you don't want to work with a yml structured file.

Finally, your third attempt is much closer to what you are looking for when it comes to the content of the file:

Hi [email protected],

You have been invited.

Cheers!

Now the only error you're bumping into is the format. Your file is still named as a YAML file which is leading your app to try to parse it as such. Hence the ActiveRecord::Fixture::FormatError error you see at this point.

Just rename this file by deleting the .yml extension and it should be fine.

You can rename it to test/fixtures/custom_mailer/message_email and then you should be able to read it on your tests using the read_fixture helper.

read_fixture("message_email").join

Alternatively, if you prefer to make it even more explicit that's a plain text file, you could also rename it to message_email.txt. And then read it by calling:

read_fixture("message_email.txt").join
发布评论

评论列表(0)

  1. 暂无评论